user779444
user779444

Reputation: 1395

WPF Activate binding OnTextInput on textbox

I have a textbox with the following binding :

<TextBox Binding={Path=MyStr, Mode=TwoWay}>

My problem is that the binding only fires when the textbox loses focus and not for each char the user enters.

The textbox also has an OnTextInput function that fires properly but how do I activate the binding in the code behind?

*I'd rather doing it in a pure mvvm way (not use the window's code behind if possible)

Any answer will be accepted mvvm or not.

Upvotes: 0

Views: 360

Answers (3)

Bearcat9425
Bearcat9425

Reputation: 1598

There is a UpdateSourceTrigger Called PropertyChanged, if you use that as your update trigger when you change the text of the textbox it will fire and in your binding property setter you can perform what ever action you need to happen as text changes.

<TextBox Text="{Binding Path=Text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />

Upvotes: 0

Patryk Ćwiek
Patryk Ćwiek

Reputation: 14318

Here:

<TextBox Text="{Binding Path=MyStr, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

now your bound property will be changed on each character.

Upvotes: 3

JMan
JMan

Reputation: 2629

U can use the updatesource trigger and set it to property changed

<Binding Source="{StaticResource myDataSource}" Path="Name"
         UpdateSourceTrigger="PropertyChanged"/>

The moment it is changed it will sent it's updated value back to your DataContext

on msdn:
http://msdn.microsoft.com/en-us/library/system.windows.data.updatesourcetrigger.aspx

Upvotes: 1

Related Questions