I have a class with a simple dependency property that I wrote with the help of the propdp
snippet in Visual Studio. I derived the class from TextBox
and gave it a PrePend
property that I want to be a dependency property. I want it so that if the user sets PrePend
to "Hello " and Text
to "World", the Text
getter will return "Hello World". My problem is that the code is seemingly ignoring my PrePend
dependency property.
My class is here:
internal class MyTextBox : TextBox
{
public new string Text
{
get { return this.PrePend + base.Text; }
set { base.Text = value; }
}
public string PrePend
{
get { return (string)GetValue(PrePendProperty); }
set { SetValue(PrePendProperty, value); }
}
// Using a DependencyProperty as the backing store for PrePend.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty PrePendProperty =
DependencyProperty.Register("PrePend", typeof(string),
typeof(MyTextBox), new PropertyMetadata(""));
}
I got this code by using the propdp
snippet. I only added the Text
setter.
My XAML
code is simply this:
<local:MyTextBox PrePend="Hello " Text="World" />
When I run the code, the screen displays "World". I put breakpoints in the Text
getter and setter, and the PrePend
getter and setter, but the code never breaks there.
What am I doing wrong with this simple example? Should I not use the propdp
code snippet to do this? I don't understand dependency properties very well.