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

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

问题描述

这个例子是:

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

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?

推荐答案

试试这个

XML

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

然后我们可以将textBox2的TextProperty的绑定设置为和textBox1一样,后面有这段代码

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天全站免登陆