0
votes

I'm using C# in a WPF application with MVVM (with Caliburn Micro framework). I'm trying to bind 2 elements (one TextBlock and one TextBox) to the same property, that resides in my model view. My property is called FirstName. I have two options to do the binding: Binding Path=FirstName or x:Name=FirstName. When I edit the textbox, I see the changes in the textblock only if I bind in a certain way (see code). Any idea of why the other way does not work? (when I type in the textbox I don't see my textblock updates)

I've tried different mode options (two ways, one way, etc). The NotifyOfPropertyChange seems to be working.

<!-- This works -->
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBox x:Name="FirstName"/>

<!-- This does not work -->
<TextBlock x:Name="FirstName"/>
<TextBox Text="{Binding Path=FirstName, Mode=TwoWay}"/>
1
What about if you use {Binding FirstName} for both? In the one that does not work, does the property get properly set by the TextBox - i.e. is it the TextBox's binding or the TextBlock's binding that isn't working? - canton7

1 Answers

1
votes

With your second example, you need to specify UpdateSourceTrigger=PropertyChanged:

<TextBlock x:Name="FirstName"/>
<TextBox Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Otherwise, the source is only updated when the TextBox loses focus.