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.
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.
- What is the version of .Net you are using?
- 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();
}
}
来源:https://stackoverflow.com/questions/20377195/superscripts-are-not-coming-in-wpf-richtext-box