jonjohnson
jonjohnson

Reputation: 423

WPF C# Textbox text change update in ViewModel

Hello I am working on a simple MVVM project; a simple text/config editor that loads a config file and then it checks in the ViewModel in case the file has been changed, it enables the Save menu item simply by binding a boolean property. But here comes a problem, where I can't find any property in the textbox control that could be bound to a vm property in case a change happens in the text. I have managed to somehow simulate this by creating an event in the code-behind :

(DataContext as AnalizeSectionViewModel).ContentChanged = true;

The event is fired on any text change. But I would like to bind a property from the textbox, something like:

IsModified="{Binding ContentChanged}"

Can such a thing be done?

Upvotes: 0

Views: 4671

Answers (1)

xspydr
xspydr

Reputation: 3060

You should be able to just bind the Text textbox property to your model via binding

Text="{Binding MyViewModelProperty}"

Anytime the text in your textbox changes your property in your model will change which will allow you to do 'stuff' when that occurs. This will fire the property changed event when the user moves to out of the field.

Now, if the intent is for it to fire each time the user types then you can explicitly tack on the

UpdateSourceTrigger="PropertyChanged"

By setting it to PropertyChanged, you will get a notification each and every time the text changes.

Upvotes: 8

Related Questions