superscripts are not coming in wpf richtext box

99封情书 提交于 2019-12-05 07:08:41

use Typography.variants:

<Paragraph FontFamily="Palatino Linotype">
  2<Run Typography.Variants="Superscript">3</Run>
  14<Run Typography.Variants="Superscript">th</Run>
</Paragraph>

I have a few questions.

  1. What is the version of .Net you are using?
  2. On what OS (Win7 or 8.1 or 10) you are seeing this issue?

This might be an known issue. However, I found the following workaround. I believe this might lead to fixing your issue.

In my example app I just added a RichTextBox and a Button, Button is bound to a Command "SuperScriptText" which is invoked when button is clicked and a CommandParameter which is bound to RichTextBox and is passed as parameter to "SuperScriptText" Command

MainWindow.xaml

<Grid>
    <StackPanel Orientation="Horizontal">
        <RichTextBox Name="rtb" Height="100" Width="300" HorizontalAlignment="Left" VerticalAlignment="Top" ></RichTextBox>
        <Button Command="{Binding SuperScriptText}" CommandParameter="{Binding ElementName=rtb}" Height="25" Width="100" HorizontalAlignment="Left" VerticalAlignment="Top" Content="SuperScript"/>
    </StackPanel>
</Grid>

In the ViemModel the important part is public ICommand SuperScriptText { get; set; } which is initialized with DelegateCommand<FrameworkElement> now here FrameworkElement allows View to pass RichTextBox as CommandParameter

MainWindowViewModel.cs

public class MainWindowViewModel : BindableBase
{
    public MainWindowViewModel()
    {
        this.SuperScriptText = new DelegateCommand<FrameworkElement>(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; }
}

And here comes the most important part providing DataContext to the MainWindow

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainWindowViewModel();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!