6

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>

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>

but its not working...

Here StaticResource is

<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 RichTextBox name.

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

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

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
user2519971
  • 345
  • 3
  • 12
  • 27
  • Anybody got the answer for this? I am also looking for the same. – Parag May 29 '17 at 14:33
  • 1
    @Parag In general you are out of luck because WPF supports subscript/superscript properly only for some (OpenType) fonts as discussed [here](https://stackoverflow.com/questions/1252161/detect-whether-a-font-supports-variants-like-superscript-and-subscript) and [here](https://stackoverflow.com/questions/1252114/superscript-subscript-in-hyperlink-in-wpf), and partially mentioned in the documentation [here](https://msdn.microsoft.com/en-us/library/system.windows.documents.typography.variants(v=vs.110).aspx) and [here](https://msdn.microsoft.com/en-us/library/ms745109(v=vs.110).aspx) – Ivan Stoev May 29 '17 at 17:55
  • 1
    oh ok.. so i have to live with this limitation. :( – Parag May 31 '17 at 12:17

3 Answers3

0

use Typography.variants:

<Paragraph FontFamily="Palatino Linotype">
  2<Run Typography.Variants="Superscript">3</Run>
  14<Run Typography.Variants="Superscript">th</Run>
</Paragraph>
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • My question is if EditingCommands.ToggleBold is working for me then why not EditingCommands.ToggleSuperscript...any idea on this ? – user2519971 Dec 04 '13 at 14:15
0

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.

Shankar
  • 176
  • 7
  • I tried Typography.variants as well, but it does not work. I have windows 7 professional 64 bit machine with .net framework 4.6.1 – Parag May 31 '17 at 12:19
0

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();
    }
}
Anurag
  • 36
  • 5