上标不是来自wpf richtext框
我在wpf mvvm应用程序中实现了一个自定义的富文本框,并提供了格式化输入文本的选项,如下所示:
我正在使用EditingCommands.ToggleBold使文本变为粗体。 以同样的方式,我给出了ToggleSuperscript的选项
但它不起作用……
这里的StaticResource是
和mainRTB是我的RichTextBox名称。
我对此毫无头绪。 任何机构都可以建议如何启用ToggleSuperscript和ToggleSubscript。
使用Typography.variants :
23 14th
我有几个问题。
- 你正在使用的.Net版本是什么?
- 在什么操作系统(Win7或8.1或10)上你看到这个问题?
这可能是一个已知问题。 但是,我发现了以下解决方法 。 我相信这可能会导致您解决问题。
在我的示例应用程序中,我刚添加了一个RichTextBox
和一个Button
, Button
被绑定到一个Command
“SuperScriptText”,它在单击按钮时调用,一个CommandParameter
绑定到RichTextBox
并作为参数传递给“SuperScriptText” Command
MainWindow.xaml
在ViemModel中,重要的部分是public ICommand SuperScriptText { get; set; }
public ICommand SuperScriptText { get; set; }
现在使用DelegateCommand
初始化FrameworkElement允许View将RichTextBox
作为CommandParameter
传递
MainWindowViewModel.cs
public class MainWindowViewModel : BindableBase { public MainWindowViewModel() { this.SuperScriptText = new DelegateCommand(SuperScriptTextCommandHandler); } private void SuperScriptTextCommandHandler(FrameworkElement obj) { var rtb = obj as RichTextBox; if (rtb != null) { var currentAlignment = rtb.Selection.GetPropertyValue(Inline.BaselineAlignmentProperty); BaselineAlignment newAlignment = ((BaselineAlignment)currentAlignment == BaselineAlignment.Superscript) ? BaselineAlignment.Baseline : BaselineAlignment.Superscript; rtb.Selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, newAlignment); } } public ICommand SuperScriptText { get; set; } }
这里是向MainWindow提供DataContext
的最重要部分
MainWindow.xaml.cs
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new MainWindowViewModel(); } }