J'ai implémenté une zone de texte enrichi personnalisée dans une application wpf mvvm et ai donné la possibilité de formater le texte saisi de la manière suivante:
<Button Style="{StaticResource formatTextStyle}"
Command="EditingCommands.ToggleBold" ToolTip="Bold">
<TextBlock FontWeight="Bold">B</TextBlock>
</Button>
J'utilise EditingCommands.ToggleBold pour rendre le texte en gras. De la même manière, je donne l'option pour ToggleSuperscript
<Button Style="{StaticResource formatImageStyle}"
Command="EditingCommands.ToggleSuperscript" ToolTip="Superscript">
<TextBlock FontStyle="Italic" FontWeight="Bold">SubScript</TextBlock>
</Button>
mais ça ne marche pas ...
Voici 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>
et mainRTB est mon nom RichTextBox.
<RichTextBox Name="mainRTB" AcceptsTab="True" Height="160"
asis:RichTextboxAssistant.BoundDocument="{Binding Path=Text,
ElementName=uxRichTextEditor}"
VerticalScrollBarVisibility="Visible" />
Je suis désemparé à ce sujet. Tout corps peut-il suggérer comment activer ToggleSuperscript et ToggleSubscript.
Réponses:
0 pour la réponse № 1utilisation Typographie.variants:
<Paragraph FontFamily="Palatino Linotype">
2<Run Typography.Variants="Superscript">3</Run>
14<Run Typography.Variants="Superscript">th</Run>
</Paragraph>
0 pour la réponse № 2
J'ai quelques questions.
- Quelle est la version de .Net que vous utilisez?
- Sur quel OS (Win7 ou 8.1 ou 10) vous voyez ce problème?
Cela pourrait être un problème connu. Cependant, j'ai trouvé ce qui suit solution de contournement. Je crois que cela pourrait conduire à résoudre votre problème.
0 pour la réponse № 3
Dans mon exemple d'application, je viens d'ajouter un RichTextBox
et un Button
, Button
est lié à un Command
"SuperScriptText" qui est invoqué lorsque le bouton est cliqué et qu'un CommandParameter
qui est lié à RichTextBox
et est passé en paramètre à "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>
Dans le ViemModel, l’important est de public ICommand SuperScriptText { get; set; }
qui est initialisé avec DelegateCommand<FrameworkElement>
maintenant ici FrameworkElement permet à View de passer RichTextBox
comme 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; }
}
Et voici la partie la plus importante fournissant DataContext
à la fenêtre principale
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}