WPF:获取属性,一个控制在code绑定到背后 [英] WPF: Get Property that a control is Bound to in code behind

查看:133
本文介绍了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;

<一个href=\"https://web.archive.org/web/20111008212650/http://blog.spencen.com/2008/05/02/how-to-get-a-list-of-bindings-in-wpf.aspx\"相对=nofollow>要给予信贷,这是由于,

这篇关于WPF:获取属性,一个控制在code绑定到背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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