[英]How to bind two elements to the same property in WPF - C#
I'm using C# in a WPF application with MVVM (with Caliburn Micro framework). 我在带有MVVM(带有Caliburn Micro框架)的WPF应用程序中使用C#。 I'm trying to bind 2 elements (one TextBlock and one TextBox) to the same property, that resides in my model view. 我试图将2个元素(一个TextBlock和一个TextBox)绑定到位于模型视图中的相同属性。 My property is called FirstName . 我的属性称为FirstName 。 I have two options to do the binding: Binding Path=FirstName or x:Name=FirstName . 我有两个选择来进行绑定: Binding Path = FirstName或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. NotifyOfPropertyChange似乎正在工作。
<!-- 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}"/>
With your second example, you need to specify UpdateSourceTrigger=PropertyChanged
: 在第二个示例中,您需要指定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. 否则,仅当TextBox失去焦点时才更新源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.