Je suis en train de créer un éditeur de texte dans WPF à l'aide d'un RichTextBox. Mon problème est de changer la taille de la police de mon texte. Mon code fonctionne comme prévu dans tous les cas, sauf lorsque le curseur est dans un mot. Dans ce cas, il ne devrait pas changer la taille de la police de quelque chose, sauf la taille de la police du texte à venir si l'utilisateur écrit quoi que ce soit. Le problème est que, pour une raison quelconque, TextSelection.ApplyPropertyValue(RichTextBox.FontSizeProperty, value)
change la taille de la police du mot entier lorsque le curseur est à l'intérieur d'un mot.Problèmes avec RichTextBox.Selection et TextSelection.ApplyPropertyValue
Ceci est mon eventHandler:
private void fontSizeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = (ComboBox)sender;
string value = (string)comboBox.SelectedValue;
if (comboBox.IsDropDownOpen)
{
TextSelection text = textBoxMain.Selection;
richTextBox.Focus();
text.ApplyPropertyValue(RichTextBox.FontSizeProperty, value);
}
}
Et je ne peux pas utiliser quelque chose comme l'ajout !text.Text.IsEmpty
dans l'instruction if, parce que je dois toujours être en mesure de changer la taille de police du texte à écrire.
J'ai trouvé des questions similaires sur StackOverFlow, mais aucune avec une réponse de travail réelle.
Edit: Ajouté XAML
<Window x:Class="MathEdit.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MathEdit"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
<CommandBinding Command="Open" Executed="OpenCommandBinding_Executed"></CommandBinding>
<CommandBinding Command="Save" Executed="SaveCommandBinding_Executed"></CommandBinding>
<CommandBinding Command="SaveAs" Executed="SaveAsCommandBinding_Executed"></CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="O" Modifiers="Control" Command="Open"></KeyBinding>
<KeyBinding Key="S" Modifiers="Control" Command="Save"></KeyBinding>
<KeyBinding Key="S" Modifiers="Control+Alt" Command="SaveAs"></KeyBinding>
</Window.InputBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" InputGestureText="Ctrl+N" />
<MenuItem Header="_Open" InputGestureText="Ctrl+O" Command="Open"/>
<MenuItem Header="_Save" InputGestureText="Ctrl+S" Command="Save"/>
<MenuItem Header="_Save As" InputGestureText="Ctrl+Alt+S" Command="SaveAs"/>
<Separator />
<MenuItem Header="_Exit" InputGestureText="Alt+F4" />
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_Check if toby = on" IsCheckable="false" IsChecked="True" Click="MenuItem_Click" />
<MenuItem Header="_Settings" Click="MenuItem_Click_2" IsCheckable="True" />
<MenuItem Header="_Add formula" x:Name="menuItemAdd" Click="MenuItem_Add_Click" />
</MenuItem>
<ComboBox x:Name="fontSizeBox" Width="40" SelectedValuePath="Content" SelectionChanged="fontSizeBox_SelectionChanged" SelectedIndex="2">
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="12"/>
<ComboBoxItem Content="16"/>
<ComboBoxItem Content="20"/>
</ComboBox>
</Menu>
<Grid x:Name="gridParent">
<RichTextBox x:Name="richTextBox" AcceptsReturn="True" SelectionChanged="textBoxMain_SelectionChanged" />
</Grid>
</DockPanel>
Y at-il une chance que vous pouvez poster le XAML aussi? –
Voici votre réponse http://stackoverflow.com/a/4199012/169714 vous appliquez la taille de police à la sélection, mais lorsque le curseur est dans un mot. il n'y a pas de sélection, vous devez donc l'appliquer au texte à venir. –
Comme également souligné par le commentaire à cette réponse, cela ne fonctionne pas. – Felza