Superscripts not included in wpf richtext box

I applied a special text field in the wpf mvvm application and made it possible to format the entered text as follows:

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

I use EditingCommands.ToggleBold to make the text bold. Similarly, I enable ToggleSuperscript

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

but its not working ...

Here is 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> 

and mainRTB is my name RichTextBox.

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

I do not know about that. Can any body suggest how to enable ToggleSuperscript and ToggleSubscript.

+6
source share
3 answers

use Typography.variants :

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

I have a few questions.

  • What version of .Net are you using?
  • In which OS (Win7 or 8.1 or 10) do you see this problem?

This may be a known issue. However, I found the following workaround . I believe that this can lead to the correction of your problem.

0
source

In my sample application, I just added a RichTextBox , and Button , Button bound to Command "SuperScriptText", which is called when the button is pressed, and CommandParameter , which is bound to RichTextBox and passed as a parameter to the "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 ViemModel, an important part of public ICommand SuperScriptText { get; set; } public ICommand SuperScriptText { get; set; } public ICommand SuperScriptText { get; set; } , which is initialized using the DelegateCommand<FrameworkElement> , now here the FrameworkElement allows the View to pass the RichTextBox as a 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 is the most important part providing the DataContext for MainWindow

MainWindow.xaml.cs

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

Source: https://habr.com/ru/post/959325/


All Articles