本文介绍了上标不会出现在 wpf 富文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have implemented a custom rich text box in a wpf mvvm application and have given option to format the entered text like this:

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

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

I am using EditingCommands.ToggleBold to make the text bold. In the same way I am giving the option for ToggleSuperscript

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

但它不起作用...

这里是静态资源

<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 名称.

and mainRTB is my RichTextBox name.

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

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

I am clueless on this. Can any body suggest how to enable ToggleSuperscript and ToggleSubscript.

推荐答案

在我的示例应用中,我刚刚添加了一个 RichTextBox 和一个 ButtonButton 绑定到一个 Command "SuperScriptText",当按钮被点击时调用,CommandParameter 绑定到 RichTextBox 并作为SuperScriptText"的参数Command

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

主窗口.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;放;}DelegateCommand 初始化,现在 FrameworkElement 允许 View 将 RichTextBox 作为 CommandParameter

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

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; }
}

这里是最重要的部分,为 MainWindow 提供 DataContext

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

MainWindow.xaml.cs

MainWindow.xaml.cs

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

这篇关于上标不会出现在 wpf 富文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:50