Vaccano
Vaccano

Reputation: 82341

Get change events for value bound to TextProperty for a decendant of TextBox

I have my own custom TextBox class. In part it helps me handle scans from a barcode reader.

Part of what I need for this to work is a way to capture when the value bound to TextProperty changes.

Note: I am not looking for the "TextChanged" event or any kind of event that relates to a key press (except when that key press updates the source).

I need an event that will fire ONLY when the underlying value gets changed, and I need to be able to access it in my TextBox (meaning it can't be in the ViewModel).

Is this possible?

Upvotes: 1

Views: 147

Answers (1)

Dante
Dante

Reputation: 3316

Yes, you can do this:

In your class' constructor:

  var dpd = DependencyPropertyDescriptor.FromProperty(YourTextBoxClass.TextProperty, typeof(YourTextBoxClass));
 if (dpd != null)
 {
     dpd.AddValueChanged(this, ThisIsCalledWhenPropertyIsChanged);
 }    

And then the handler:

private void ThisIsCalledWhenPropertyIsChanged(object sender, EventArgs e) { }

Hope it helps,

Regards

Upvotes: 2

Related Questions