开发者

wpf:Rich text box runtime under line, not Woking while get back from database

I am using the WPF RichTextBox. I do run time formatting such as make under line. It normally works fine. Bu开发者_JAVA技巧t after reload from database it is not working properly. This is the sample code what I am using.

var selection = TxtRtf1.Selection;
if (!selection.IsEmpty)
{
   var tdc =(TextDecorationCollection) selection.GetPropertyValue(Inline.TextDecorationsProperty);
   if (tdc == null || !tdc.Equals(TextDecorations.Underline))
      selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
   else
      selection.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
}

Actual what happen <tdc.Equals(TextDecorations.Underline)> return always false; after data read from database.


Try tdc.SequenceEqual(TextDecorations.Underline).

 var tdc =(TextDecorationCollection) selection.GetPropertyValue(Inline.TextDecorationsProperty);
 if (tdc == null || !tdc.SequenceEqual(TextDecorations.Underline))
    selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
 else
    selection.ApplyPropertyValue(Inline.TextDecorationsProperty, null);


The solution provided here seems to work for me. I've modified it ever so slightly. Of course, you'll still need to use data binding or some other method to display the boolean information to the user.

private bool RTEHasUnderlinedText()
    {
        var sel = richTextBox.Selection;
        var value = GetPropertyValue(sel, Paragraph.TextDecorationsProperty);
        return ((TextDecorationCollection)(value)).Count > 0;
    }

    private Object GetPropertyValue(TextRange textRange, DependencyProperty formattingProperty)
    {
        Object value = null;
        var pointer = textRange.Start;
        if (pointer is TextPointer)
        {
            Boolean needsContinue = true;
            DependencyObject element = ((TextPointer)pointer).Parent as TextElement;
            while (needsContinue && (element is Inline || element is Paragraph || element is TextBlock))
            {
                value = element.GetValue(formattingProperty);
                IEnumerable seq = value as IEnumerable;
                needsContinue = (seq == null) ? value == null : seq.Cast<Object>().Count() == 0;
                element = element is TextElement ? ((TextElement)element).Parent : null;
            }
        }
        return value;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜