【发布时间】: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