C# WPF依赖属性-数据绑定不';行不通
正如标题所说,我在使用DependencyProperty的数据绑定时遇到了问题。我有一个叫做HTMLBox的类:C# WPF依赖属性-数据绑定不';行不通,c#,wpf,data-binding,dependency-properties,C#,Wpf,Data Binding,Dependency Properties,正如标题所说,我在使用DependencyProperty的数据绑定时遇到了问题。我有一个叫做HTMLBox的类: public class HTMLBox : RichTextBox { public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(HTMLBox)); public string
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;
}
}
我正在读取构造函数中的Text属性,因此当字符串绑定到该属性时,它应该显示为文本。但是,即使我将一些数据绑定到xaml中的Text属性,我甚至看不到“Setter…”消息,设置Text属性时应该显示该消息
<local:HTMLBox Text="{Binding Text}"
Width="{Binding Width}"
AcceptsReturn="True"
Height="{Binding Height}" />
如果我将HTMLBox更改为TextBox,文本将正确显示,因此错误可能在HTMLBox类中的某个位置。我做错了什么?这里有一些问题:
dependencProperty.register
注册依赖项属性时,通过更改事件处理程序执行此操作Text
属性发生更改时,重建/更新UI谢谢,使用更改事件处理程序解决了这个问题。似乎我在这里有很多东西要学:)