[英]WPF: Get Property that a control is Bound to in code behind
我试图找到一种方法来获取控件绑定的属性(在c#中)。
如果我有以下内容:
<dxe:ComboBoxEdit DisplayMember="Name" ItemsSource="{Binding Path=NameOptions, Mode=OneTime}" SelectedItem="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" />
我现在正试图获取SelectedItem绑定的位置,即结果应该是“Name”。 然后在代码中我需要用ViewModel属性做一些事情。 问题是我不能只对它进行硬编码,因为它是一种需要处理表单上每个控件的通用方法。
谢谢,理查德
我认为应该这样做:
BindingExpression be = BindingOperations.GetBindingExpression((FrameworkElement)yourComboBox, ((DependencyProperty)Button.SelectedItemProperty));
string Name = be.ParentBinding.Path.Path;
IE:
var bindingExpression = this.myComboBox.GetBindingExpression(ComboBox.SelectedItem);
string bindingPath = bindingExpression.ParentBinding.Path.Path
我看到你使用的是DXE ComboBox而不是标准 - 期望它来自普通的.NET控件对象,你仍然应该拥有这个功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.