简体   繁体   English

是否可以将两个源属性绑定到一个控件属性?

[英]Is it possible to bind two source properties to one control property?

Using WPF, is it possible to bind two source properties, via one-way binding, to one control property (ie textbox.text)?? 使用WPF,是否可以通过单向绑定将两个源属性绑定到一个控件属性(即textbox.text)? I'd like to have the user input a value in a textbox and have that value pushed to two different properties in the source datacontext. 我想让用户在文本框中输入一个值,并将该值推送到源数据上下文中的两个不同属性。 My workaround is to create a dummy property and have the setter set the two values manually. 我的解决方法是创建一个虚拟属性,并让设置器手动设置两个值。

You need to use a MultiBinding with a IMultiValueConverter . 您需要将MultiBindingIMul​​tiValueConverter一起使用。 See the example in the documentation 请参阅文档中的示例

You could use kind of binding-proxy, as described in this blog post: 您可以使用一种绑定代理,如此博客文章所述:

http://www.11011.net/wpf-binding-properties http://www.11011.net/wpf-binding-properties

Then just follow these steps for example: 然后,请按照以下步骤操作:

  1. Define a TextBox with x:Name=tb 用x:Name = tb定义一个文本框
  2. Bind TextBox.Text to Prop1 将TextBox.Text绑定到Prop1
  3. Add a Proxy-Element with In="{Binding Text, ElementName=tb}", Out="{Binding Prop2}" (not within but beside the TextBox) 添加带有In =“ {Binding Text,ElementName = tb}”,Out =“ {Binding Prop2}”的代理元素(不在TextBox中,但在TextBox旁边)

This way, you will have Prop1 and Prop2 synchronized with tb.Text. 这样,您将使Prop1和Prop2与tb.Text同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM