Search code examples
wpfeventstextchanged

wpf: TextChanged event fired on setting DataContext


I've got a simple View with a single textbox that gets databound to a simple ViewModel with a single string property.

I need to catch the TextChanged event of that textbox so that I can do a little validation magic.

The problem that I am running into is that the TextChanged event fires for that textbox when the DataContext is set for the View.

Is there a standard mechanism that I can use to determine if the event is firing because of the DataContext being set versus when the user is making changes?

Thanks!


Solution

  • As far as I know there is no such mechanism. What you should do instead is to do your validation magic using standard means of WPF. Please see the following link: http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_validation.

    Anyway, as long as you use MVVM you can always detect that text has changed in the setter of the bound property in your view model.