【发布时间】:2016-04-15 01:59:03
【问题描述】:
我的richtextbox 有一个奇怪的问题。 我想检测文本何时为粗体、斜体等。
例如
if (richTextBox.Selection.GetPropertyValue(TextElement.FontStyleProperty).ToString() == "Italic") // Pochylenie
{
heremycode
}
如果我们使用
MessageBox(richTextBox.Selection.GetPropertyValue(TextElement.FontStyleProperty).ToString());
我得到斜体。我想对下划线和删除线做同样的事情,因为我不能使用
TextBlock.TextDecorationsProperty.ToString(),
因为我只得到我认为的方法的名称?不要想像“斜体”或“粗体”只是“FontStyleProperty”。
private void richTextBox_SelectionChanged(object sender, RoutedEventArgs e)
{
....
if (richTextBox.Selection.GetPropertyValue(TextElement.FontStyleProperty).ToString() == "Italic"
{
backgroundP.Stroke = Brushes.Black;
backgroundP.Fill = Brushes.LawnGreen;
p = true;
}
TextRange selectionRange = new TextRange(richTextBox.Selection.Start, richTextBox.Selection.End);
if (selectionRange.GetPropertyValue(Underline.TextDecorationsProperty).Equals(TextDecorations.Underline))
{
MessageBox.Show("Wow We did it :)");
backgroundUnderline.Stroke = Brushes.Black;
}
}
和 XAML 代码:
<Grid x:Name="Center" Margin="10,231,10,10">
<Rectangle Fill="#B2F4F4F5" Stroke="Black" Margin="1,0,-1,0"/>
<Label x:Name="labelNotepad" Content="Notepad" HorizontalAlignment="Left" VerticalAlignment="Top" Width="385" FontWeight="Bold" Background="#FFC1FCFF" FontSize="21.333" Height="56" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Margin="2,1,0,0" BorderThickness="0,0,1,0"/>
<RichTextBox x:Name="richTextBox" Margin="1,58,0,0" FontSize="16" BorderThickness="1,2,1,1" BorderBrush="Black" UseLayoutRounding="False" VerticalScrollBarVisibility="Auto" Background="#7FFFFFFF" SelectionChanged="richTextBox_SelectionChanged">
<FlowDocument/>
</RichTextBox>
【问题讨论】:
-
您的尝试结果如何?异常或空字符串等?
-
-
您能否将此添加到您的问题和屏幕截图或其他有帮助的内容中
-
我编辑了我的问题
标签: wpf richtextbox