RichTextBox
oneと oneを含む Grid で構成される非常に単純な WPF Window を作成しましたComboBox
。を使用して、選択ComboBox
範囲のフォント サイズを変更および検索します。RichTextBox
私の XAML のコード ビハインド ファイルは次のとおりです。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Add the font sizes.
for (var i = 1; i < 72; i++)
{
FontSize.Items.Add((double) i);
}
}
private void MyTextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
// If the selection changes, update the font size in the ComboBox.
FontSize.SelectedValue = (double) MyTextBox.Selection.GetPropertyValue(TextBlock.FontSizeProperty);
}
private void FontSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If the selected size changes, change the size of the selection in the RichTextBox.
if (FontSize.SelectedItem != null)
MyTextBox.Selection.ApplyPropertyValue(TextBlock.FontSizeProperty, FontSize.SelectedItem);
}
}
ここには 2 つのことがあります。
MyTextBox_SelectionChanged
ComboBox
選択範囲のフォント サイズで を更新します。FontSize_SelectionChanged
選択範囲のフォントサイズを変更します。
以下の問題を確認できます。
選択してフォントサイズを変更すると、完全に変更されます。しかし、フォントサイズが異なる別のテキストをクリックすると、元に戻ります。
この動作の原因は何ですか?
編集: XAML ファイルは次のとおりです。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="FontSize" HorizontalAlignment="Left" VerticalAlignment="Top" Width="497" Margin="10,10,0,0" SelectionChanged="FontSize_SelectionChanged"/>
<RichTextBox x:Name="MyTextBox" HorizontalAlignment="Left" Height="273" VerticalAlignment="Top" Width="497" Margin="10,37,0,0" RenderTransformOrigin="0.358,0.48" SelectionChanged="MyTextBox_SelectionChanged">
<FlowDocument>
<Paragraph>
<Run Text="RichTextBox"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Window>
編集2:デバッグ中に行ったことの簡単な説明は次のとおりです。
MyTextBox_SelectionChanged
に 1 つ、に 1 つの 2 つのデバッグ ポイントがありますFontSize_SelectionChanged
。- フォント サイズを変更するときは、F5 をクリックして続行します。
- テキストの別の部分 (デフォルト サイズ) をクリックすると、
MyTextBox_SelectionChanged
が呼び出されます。はSelection.Text
空です。 - その後、再び続行し、 への呼び出しで停止し
FontSize_SelectionChanged
ます。しかし、まだSelection.Text
空ですが、私の古い選択「リッチ」は古いフォントサイズに戻ります。
編集 3:この問題は、Sams Teach Yourself WPF in 2008 年 7 月の最初の印刷、135 ページ、「テキスト エディターを期待どおりに機能させる」、項目 9 で言及されています。その特定の問題。