【发布时间】:2011-08-09 09:53:18
【问题描述】:
正如标题所说,我在使用具有 DependencyProperty 的数据绑定时遇到了问题。我有一个名为 HTMLBox 的类:
public class HTMLBox : RichTextBox
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(HTMLBox));
public string Text
{
get
{
return GetValue(TextProperty) as string;
}
set
{
Console.WriteLine("Setter...");
SetValue(TextProperty, value);
}
}
public HTMLBox()
{
// Create a FlowDocument
FlowDocument mcFlowDoc = new FlowDocument();
// Create a paragraph with text
Paragraph para = new Paragraph();
para.Inlines.Add(new Bold(new Run(Text)));
// Add the paragraph to blocks of paragraph
mcFlowDoc.Blocks.Add(para);
this.Document = mcFlowDoc;
}
}
我在构造函数中读取文本属性,因此当字符串绑定到属性时,它应该显示为文本。但即使我将一些数据绑定到 xaml 中的 Text 属性,我什至看不到设置 Text 属性时应该显示的“Setter...”-Message。
<local:HTMLBox Text="{Binding Text}"
Width="{Binding Width}"
AcceptsReturn="True"
Height="{Binding Height}" />
如果我将 HTMLBox 更改为 TextBox,则文本会正确显示,因此错误可能出在我的 HTMLBox 类的某个地方。我做错了什么?
【问题讨论】:
标签: c# wpf data-binding dependency-properties