I have created a CustomTextBox which extends TextBox.
I have overridden the Metadata for TextProperty because I want to trigger a method when the text changes, but when I run my code and edit the textbox, I am only seeing the OnTextPropertyChanged event fired twice. First time when I initialise the textbox, and the second when I close the window.
How can I have this fired for each keystroke in the Textbox?
AppTextBox.cs:
public class AppTextBox : TextBox
{
static AppTextBox()
{
TextProperty.OverrideMetadata(typeof(AppTextBox),
new FrameworkPropertyMetadata(
string.Empty,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
OnTextPropertyChanged,
null,
true,
UpdateSourceTrigger.PropertyChanged)
);
DefaultStyleKeyProperty.OverrideMetadata(typeof(AppTextBox), new FrameworkPropertyMetadata(typeof(AppTextBox)));
}
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// TODO: Check if Text is null or empty...
}
}
Usage:
<ctrl:AppTextBox Text="{Binding NewWord.WrittenForm}" />
So when I first load the textbox, the content is "Foo".
When I add or delete a letter, I would expect the OnTextPropertyChanged to be fired, but it is not. Should I be doing something different to achieve my goal?
This
propdpcan be simplified.The trick is to provide binding on the derived control
and here's the full control