阅读背景:

自定义WPF控件,统一无法解决

来源:互联网 
public class RichTextBoxExtended : RichTextBox
{
    static RichTextBoxExtended()
    {
        //DefaultStyleKeyProperty.OverrideMetadata(typeof(RichTextBoxExtended), new FrameworkPropertyMetadata(typeof(RichTextBoxExtended)));
    }

    public byte[] Text
    {
        get { return (byte[])GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(byte[]), typeof(RichTextBoxExtended), new UIPropertyMetadata(null, new PropertyChangedCallback(TextChangedCallback)));

    private static void TextChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        RichTextBoxExtended richTextBoxExtended = obj as RichTextBoxExtended;
        richTextBoxExtended.ChangeText(e);
    }

    private void ChangeText(DependencyPropertyChangedEventArgs e)
    {
        //clear out any formatting properties
        TextRange range = new TextRange(base.Document.ContentStart, base.Document.ContentEnd);
        range.ClearAllProperties();

        //load bytes into stream, load stream into range
        MemoryStream stream = new MemoryStream(e.NewValue as byte[]);
        range.Load(stream, DataFormats.Rtf);
    }
}
public class RichTextBoxExtended : RichTextBox



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: