WPF:获取属性,一个控制在code绑定到背后

问题描述:

我试图找到一种方式来获得的属性。它控件绑定(在C#)。

I am trying to find a way to get the Property to which a control is bound (in c#).

如果我有以下几点:

<dxe:ComboBoxEdit DisplayMember="Name" ItemsSource="{Binding Path=NameOptions, Mode=OneTime}" SelectedItem="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" />

我现在试图去其中的SelectedItem绑定到,即结果应该是名的位置。然后在code,我需要做一些东西与视图模型属性。问题是,我不能只是很难code这个,因为它是一个需要与表单上的每个控件工作的一个通用的方法。

I am now trying to get the location to which the SelectedItem is bound to, i.e. the result should be "Name". Then in code I need to do some stuff with that ViewModel Property. Issue is that I can't just hard code this as it is a generic method that needs to work with each control on the form.

谢谢,
理查德

Thanks, Richard

我觉得这个应该这样做:

I think this should do it:

BindingExpression be = BindingOperations.GetBindingExpression((FrameworkElement)yourComboBox, ((DependencyProperty)Button.SelectedItemProperty));
string Name = be.ParentBinding.Path.Path;

要给予信贷,这是由于,