老周:当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
里面代码都是抄老周的
![这里写图片描述](https://box.kancloud.cn/2016-04-08_5707636b8545f.jpg "")
参考:[http://www.cnblogs.com/tcjiaan/p/4937301.html](http://www.cnblogs.com/tcjiaan/p/4937301.html)
- 前言
- UWP win10 app 新关键字x:Bing
- win10应用 UWP 使用MD5算法
- win10 UWP读写文件
- UWP appButtonBar样式
- C# 6.0 $&quot;Hello {csdn}&quot;
- Win10 UWP xaml 延迟加载元素
- UWP xaml 圆形头像
- UWP 绘制图形
- win10 uwp 通知Toast
- win10 UWP 显示地图
- win10 uwp 参考
- win10 uwp clone
- win10 uwp 装机必备应用 含源代码
- RichEditBox 使用自定义菜单
- win10 UWP FlipView
- win10 UWP 获取系统信息
- win10 UWP 申请微软开发者
- win10 UWP button
- win10 UWP Markdown 含源代码
- win10 UWP 应用设置
- win10 UWP 九幽数据分析
- win10 UWP 圆形等待
- win10 UWP 标题栏后退
- win10 UWP 单元测试
- win10 UWP 你写我读
- win10 UWP RSS阅读器
- win10 UWP MessageDialog 和 ContentDialog
- win10 UWP Hmac
- win10 UWP GET Post
- Win10 UWP Intro to controls and events
- win10 UWP Controls by function
- win10 uwp App-to-app communication 应用通信