在WPF中如何从代码中获取具体项目的绑定? [英] In WPF how to get binding of a specific item from the code?

查看:444
本文介绍了在WPF中如何从代码中获取具体项目的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个例子是:

文本框绑定到一些数据。有一个第二个文本框不绑定到任何东西。所以我想将文本框2绑定到相同的数据第一个textBox被绑定。

A textBox is bound to some data. There is a second text box which is not bind to anything. So I want to bind text box 2 to the same data 1st textBox is bound.

换句话说,我不知道DependencyObject是否存储对它的数据的一些引用绑定?如果没有,找出特定对象的所有数据绑定的方法是什么?

In other words I wan't to know if the DependencyObject stores some reference to it's data-bindings? If not, what is the way to find out all data-bindings of a specific object?

推荐答案

尝试这个

Xaml

<TextBox Name="textBox1" Text="{Binding Text1}"/>
<TextBox Name="textBox2" Text="No Binding"/>

然后我们可以将TextBox的TextBox的绑定设置为与textBox1相同, / p>

Then we can set the binding of the TextProperty for textBox2 to the same as textBox1 with this code behind

BindingExpression bindingExpression = textBox1.GetBindingExpression(TextBox.TextProperty);
Binding parentBinding = bindingExpression.ParentBinding;
textBox2.SetBinding(TextBox.TextProperty, parentBinding);

这篇关于在WPF中如何从代码中获取具体项目的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆