在 RichTectBox 选择中检测多个 TextDecorations
Detect multiple TextDecorations in RichTectBox selection
我正在尝试将文本编辑器作为 WPF/C# 学习项目。
我有 3 个切换按钮(下划线、上划线和删除线)。
- 这些可以组合出现,但我如何检测它们?
我的 selectionChanged 事件处理程序:
private void rt_SelectionChanged(object sender, RoutedEventArgs e)
{
bool IsTextUnderline = false;
bool IsTextStrikethrough = false;
bool IsOverline = false;
TextRange range = new TextRange(rt.Selection.Start, rt.Selection.End);
var decor = range.GetPropertyValue(Inline.TextDecorationsProperty);
if (decor != DependencyProperty.UnsetValue)
{
TextDecorationCollection coll = (TextDecorationCollection)decor;
IsTextStrikethrough = (coll.Contains(TextDecorations.Strikethrough));
但是 .Contains() 需要一个 TextDecorationCollection 作为参数...
- 完全令人困惑
我很接近(只是错过了 [0] - 不管是什么意思),这是有效的:
private void rt_SelectionChanged(object sender, RoutedEventArgs e)
{
Object temp = rt.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
if (temp.ToString() != "{DependencyProperty.UnsetValue}") // multivalue, can't handle..?
{
TextDecorationCollection decs = (TextDecorationCollection)temp;
btnUnderl.IsChecked = decs.Contains(TextDecorations.Underline[0]);
btnStrike.IsChecked = decs.Contains(TextDecorations.Strikethrough[0]);
btnBaseli.IsChecked = decs.Contains(TextDecorations.Baseline[0]);
btnOverli.IsChecked = decs.Contains(TextDecorations.OverLine[0]);
}
}
我正在尝试将文本编辑器作为 WPF/C# 学习项目。
我有 3 个切换按钮(下划线、上划线和删除线)。
- 这些可以组合出现,但我如何检测它们?
我的 selectionChanged 事件处理程序:
private void rt_SelectionChanged(object sender, RoutedEventArgs e)
{
bool IsTextUnderline = false;
bool IsTextStrikethrough = false;
bool IsOverline = false;
TextRange range = new TextRange(rt.Selection.Start, rt.Selection.End);
var decor = range.GetPropertyValue(Inline.TextDecorationsProperty);
if (decor != DependencyProperty.UnsetValue)
{
TextDecorationCollection coll = (TextDecorationCollection)decor;
IsTextStrikethrough = (coll.Contains(TextDecorations.Strikethrough));
但是 .Contains() 需要一个 TextDecorationCollection 作为参数...
- 完全令人困惑
我很接近(只是错过了 [0] - 不管是什么意思),这是有效的:
private void rt_SelectionChanged(object sender, RoutedEventArgs e)
{
Object temp = rt.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
if (temp.ToString() != "{DependencyProperty.UnsetValue}") // multivalue, can't handle..?
{
TextDecorationCollection decs = (TextDecorationCollection)temp;
btnUnderl.IsChecked = decs.Contains(TextDecorations.Underline[0]);
btnStrike.IsChecked = decs.Contains(TextDecorations.Strikethrough[0]);
btnBaseli.IsChecked = decs.Contains(TextDecorations.Baseline[0]);
btnOverli.IsChecked = decs.Contains(TextDecorations.OverLine[0]);
}
}