Reputation: 219
As the title already says I am having trouble using databinding with a DependencyProperty. I have a class called 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;
}
}
I reading the Text-property in the Constructor, so it should be displayed as text when a string is bound to the property. But even though I bind some data to the Text property in xaml, I don't even see the "Setter..."-Message which should be shown when the Text-property is set.
<local:HTMLBox Text="{Binding Text}"
Width="{Binding Width}"
AcceptsReturn="True"
Height="{Binding Height}" />
If I change HTMLBox to TextBox the text is displayed properly, so the mistake is probably somwhere in my HTMLBox class. What am I doing wrong?
Upvotes: 4
Views: 1293
Reputation: 70160
You have a few issues going on here:
DependencyProperty.Register
.Text
will always be the default value in the constructor. Again, a similar solution to (1), when your Text
property changes, rebuild / update your UI.Upvotes: 4