【问题标题】:User control with richTextBox, bindable richTextBox带有richTextBox的用户控件,可绑定的richTextBox
【发布时间】:2011-01-08 18:59:52
【问题描述】:

我尝试使用richTextBox 进行用户控制,因为我需要可绑定的richTextbox。

我在这里找到了一些解决方案:Richtextbox wpf binding

我想使用 Arcturus 的解决方案。使用richTextBox控件创建用户控件并使用依赖属性。

在 XAML 中我只有richTextBox 控件:

<UserControl x:Class="WpfApplication2.BindableRichTextBoxControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <RichTextBox Name="RichTextBox" Grid.Row="0"/>    
    </Grid>
</UserControl>

在代码隐藏中:

public partial class BindableRichTextBoxControl : UserControl
{
    public static readonly DependencyProperty DocumentProperty =
    DependencyProperty.Register("Document", typeof(FlowDocument), typeof(BindableRichTextBoxControl), 
    new PropertyMetadata(OnDocumentChanged));

    public FlowDocument Document
    {
        get { return (FlowDocument)GetValue(DocumentProperty); }
        set { SetValue(DocumentProperty, value); }
    }

    private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = (BindableRichTextBoxControl)d;
        if (e.NewValue == null)
            control.RichTextBox.Document=new FlowDocument();

        //?
        control.RichTextBox.Document = document;
    }


    public BindableRichTextBoxControl()
    {
        InitializeComponent();
    }
}

我对 OnDocumentChanged 方法的最后一行有点困惑。

        control.RichTextBox.Document = document;

我无法识别什么是变量文档

【问题讨论】:

    标签: wpf richtextbox flowdocument


    【解决方案1】:

    我认为他的意思是:

    private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        RichTextBoxControl control = (RichTextBoxControl) d;
        if (e.NewValue == null)
            control.RTB.Document = new FlowDocument(); //Document is not amused by null :)
        else
            control.RTB.Document = e.NewValue;
    }
    

    但我建议您对他的原始答案发表评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-25
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      相关资源
      最近更新 更多