【问题标题】:superscripts are not coming in wpf richtext boxwpf richtextbox中没有上标
【发布时间】:2013-12-21 01:08:22
【问题描述】:

我在 wpf mvvm 应用程序中实现了一个自定义富文本框,并提供了将输入文本格式化的选项,如下所示:

<Button Style="{StaticResource formatTextStyle}"
        Command="EditingCommands.ToggleBold" ToolTip="Bold">
   <TextBlock FontWeight="Bold">B</TextBlock>
</Button>

我正在使用 EditingCommands.ToggleBold 使文本变为粗体。以同样的方式,我提供了 ToggleSuperscript 的选项

<Button Style="{StaticResource formatImageStyle}" 
        Command="EditingCommands.ToggleSuperscript" ToolTip="Superscript">
   <TextBlock FontStyle="Italic" FontWeight="Bold">SubScript</TextBlock>
</Button>

但它不工作......

这里的StaticResource是

<Style TargetType="{x:Type Button}" x:Key="formatTextStyle">
   <Setter Property="FontFamily" Value="Palatino Linotype"></Setter>
   <Setter Property="Width" Value="30"></Setter>
   <Setter Property="FontSize" Value ="14"></Setter>
   <Setter Property="CommandTarget" Value="{Binding ElementName=mainRTB}"/>
</Style>

mainRTB 是我的 RichTextBox 名称。

<RichTextBox Name="mainRTB" AcceptsTab="True" Height="160"
             asis:RichTextboxAssistant.BoundDocument="{Binding Path=Text, 
                                             ElementName=uxRichTextEditor}"
             VerticalScrollBarVisibility="Visible" />

我对此一无所知。任何机构都可以建议如何启用 ToggleSuperscript 和 ToggleSubscript。

【问题讨论】:

  • 有人得到这个答案吗?我也在找一样的。
  • @Parag 一般来说,你很不走运,因为 WPF 仅对某些 (OpenType) 字体正确支持下标/上标,如 herehere 所讨论的,并在文档 here 中部分提及和here
  • 哦,好吧..所以我必须忍受这个限制。 :(

标签: c# wpf mvvm richtextbox


【解决方案1】:

使用Typography.variants:

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

【讨论】:

  • 我的问题是如果 EditingCommands.ToggleBold 对我有用,那么为什么不 EditingCommands.ToggleSuperscript...对此有任何想法?
【解决方案2】:

我有几个问题。

  1. 您使用的 .Net 版本是什么?
  2. 您在哪个操作系统(Win7 或 8.1 或 10)上遇到此问题?

这可能是一个已知问题。但是,我发现了以下workaround。我相信这可能会解决您的问题。

【讨论】:

  • 我也尝试过 Typography.variants,但它不起作用。我有 .net 框架 4.6.1 的 windows 7 专业 64 位机器
【解决方案3】:

在我的示例应用程序中,我刚刚添加了一个RichTextBox 和一个ButtonButton 绑定到一个Command“SuperScriptText”,它在单击按钮时调用,CommandParameter 绑定到@ 987654327@ 并作为参数传递给“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>

在 ViemModel 中,重要的部分是 public ICommand SuperScriptText { get; set; },它使用 DelegateCommand&lt;FrameworkElement&gt; 初始化,现在 FrameworkElement 允许 View 将 RichTextBox 传递为 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; }
}

这是向主窗口提供DataContext 的最重要部分

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainWindowViewModel();
    }
}

【讨论】:

  • 更多详情请参考代码项目here
  • 请解释您的答案为什么它有效或为什么无效。
猜你喜欢
  • 1970-01-01
  • 2010-09-25
  • 2010-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
相关资源
最近更新 更多