在RichTectBox选择中检测多个TextDecorations

时间:2016-09-09 21:22:38

标签: wpf richtextbox text-decorations

我正在尝试将文本编辑器作为WPF / C#学习项目 我有3个togglebuttons(下划线,上划线和删除线) - 这些可以组合出现,但我如何检测它们?

我的选择改变事件处理程序:

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作为参数...
- 完全混乱

1 个答案:

答案 0 :(得分:0)

我很接近(只是错过了[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]);
    }
}