RichEditBox 使用自定义菜单

老周:当RichEditBox控件的上下文菜单即将弹出时,会引发ContextMenuOpening事件,我们需要处理该事件,并且将e.Handled属性设置为true,这样才能阻止默认上下文菜单的弹出

在RichEditBox控件上声明附加的菜单项

<RichEditBox Name="redit" Grid.Row="3" Margin="10,10,10,10" ContextMenuOpening="OnContextMenuOpening">
            <FlyoutBase.AttachedFlyout>
                <MenuFlyout>
                    <MenuFlyoutItem Text="复制" Click="OnCopy"/>
                    <MenuFlyoutItem Text="剪切" Click="OnCut"/>
                    <MenuFlyoutItem Text="粘贴" Click="OnPaste"/>
                    <MenuFlyoutSeparator/>
                    <MenuFlyoutSubItem Text="字号">
                        <MenuFlyoutItem Text="16" Tag="16" Click="OnFontSize" />
                        <MenuFlyoutItem Text="20" Tag="20" Click="OnFontSize"/>
                        <MenuFlyoutItem Text="24" Tag="24" Click="OnFontSize" />
                        <MenuFlyoutItem Text="36" Tag="36" Click="OnFontSize"/>
                        <MenuFlyoutItem Text="48" Tag="48" Click="OnFontSize"/>
                    </MenuFlyoutSubItem>
                    <!--分割-->
                    <MenuFlyoutSeparator/>
                    <ToggleMenuFlyoutItem Text="加粗" Click="OnBold" />
                    <MenuFlyoutSeparator/>
                    <MenuFlyoutSubItem Text="下划线">
                        <MenuFlyoutItem Text="无" Tag="-1" Click="OnUnderline" />
                        <MenuFlyoutItem Text="单实线" Tag="0" Click="OnUnderline"/>
                        <MenuFlyoutItem Text="双实线" Tag="1" Click="OnUnderline"/>
                        <MenuFlyoutItem Text="虚线" Tag="2" Click="OnUnderline"/>
                    </MenuFlyoutSubItem>
                    <MenuFlyoutSeparator/>
                    <MenuFlyoutSubItem Text="颜色">
                        <MenuFlyoutItem Text="黑色" Tag="黑色" Click="OnTinct"/>
                        <MenuFlyoutItem Text="蓝色" Tag="蓝色" Click="OnTinct"/>
                        <MenuFlyoutItem Text="白色" Tag="白色" Click="OnTinct"/>
                    </MenuFlyoutSubItem>
                </MenuFlyout>
            </FlyoutBase.AttachedFlyout>
        </RichEditBox>

处理OnContextMenuOpening

        private void OnContextMenuOpening(object sender , ContextMenuEventArgs e)
        {
            //阻止弹出默认的上下文菜单,然后,调用ShowAt方法在指定的坐标处打开菜单
            e.Handled = true;
            MenuFlyout menu = FlyoutBase.GetAttachedFlyout(redit) as MenuFlyout;
            menu?.ShowAt(redit , new Point(e.CursorLeft , e.CursorTop));
        }

处理复制粘贴

private void OnCopy(object sender , RoutedEventArgs e)
        {
            //复制
            redit.Document.Selection.Copy();
        }

        private void OnCut(object sender , RoutedEventArgs e)
        {
            //剪切
            redit.Document.Selection.Cut();
        }

        private void OnPaste(object sender , RoutedEventArgs e)
        {
            //粘贴
            redit.Document.Selection.Paste(0);
            //Paste 要在粘贴操作中使用的剪贴板格式。零表示最佳格式
        }

处理OnFontSize

        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnFontSize(object sender , RoutedEventArgs e)
        {
            MenuFlyoutItem item = sender as MenuFlyoutItem;
            // 获取字号
            float size = Convert.ToSingle(item.Tag);

            redit.Document.Selection.CharacterFormat.Size = size;
        }
 /// <summary>
        /// 加粗
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBold(object sender , RoutedEventArgs e)
        {
            //using Windows.UI.Text;
            ToggleMenuFlyoutItem item = sender as ToggleMenuFlyoutItem;
            redit.Document.Selection.CharacterFormat.Bold = item.IsChecked ? FormatEffect.On : FormatEffect.Off;
        }

        private void OnUnderline(object sender , RoutedEventArgs e)
        {
            MenuFlyoutItem item = sender as MenuFlyoutItem;
            int x = Convert.ToInt32(item.Tag);
            UnderlineType unlinetp;
            switch (x)
            {
                case -1: // 无
                    unlinetp = UnderlineType.None;
                    break;
                case 0: // 单实线
                    unlinetp = UnderlineType.Single;
                    break;
                case 1: // 双实线
                    unlinetp = UnderlineType.Double;
                    break;
                case 2: // 虚线
                    unlinetp = UnderlineType.Dash;
                    break;
                default:
                    unlinetp = UnderlineType.None;
                    break;
            }
            redit.Document.Selection.CharacterFormat.Underline = unlinetp;
        }
        private void OnTinct(object sender , RoutedEventArgs e)
        {
            MenuFlyoutItem item = sender as MenuFlyoutItem;
            string tinct = item.Tag as string;
            Windows.UI.Color color = new Windows.UI.Color();
            switch (tinct)
            {
                case "黑色":
                    color= Windows.UI.Colors.Black;
                    break;
                case "蓝色":
                    color = Windows.UI.Colors.Blue;
                    break;
                case "白色":
                    color = Windows.UI.Colors.White;
                    break;
                default:
                    break;
            }
            redit.Document.Selection.CharacterFormat.BackgroundColor = color;
        }

颜色在Windows.UI.Color

里面代码都是抄老周的

这里写图片描述

参考:http://www.cnblogs.com/tcjiaan/p/4937301.html

lindexi_gd CSDN认证博客专家 C# WPF UWP 微软最具价值专家
我是微软Windows应用开发方向的最具价值专家,欢迎访问我博客blog.lindexi.com里面有大量WPF和UWP博客
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页