首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过PropertyPath获取属性

通过PropertyPath获取属性
EN

Stack Overflow用户
提问于 2012-10-08 14:28:28
回答 1查看 516关注 0票数 2

此论坛条目(http://stackoverflow.com/questions/2767557/wpf-get-property-that-a-control-is-bound-to-in-code-behind)提供了以下语句,以从"bound from“PropertyPath获取"bound to”DependencyProperty:

代码语言:javascript
复制
BindingExpression be = BindingOperations.GetBindingExpression((FrameworkElement)yourComboBox, ((DependencyProperty)Button.SelectedItemProperty));
string Name = be.ParentBinding.Path.Path; 

我想更进一步--从那个PropertyPath中找到DependencyProperty。有什么标准的方法可以做到这一点吗?最终目标是在行为中使用它来删除现有绑定(AssociatedObject.PropertyPath到smth),并替换为两个绑定(Behavior.Original到smth和AssociatedObject.PropertyPath到Behavior.Modified)。

EN

回答 1

Stack Overflow用户

发布于 2013-12-18 21:17:29

您可以使用反射按名称获取依赖属性:

代码语言:javascript
复制
public static class ReflectionHelper
{

    public static DependencyProperty GetDependencyProperty(this FrameworkElement fe, string propertyName)
    {
        var propertyNamesToCheck = new List<string> { propertyName, propertyName + "Property" };
        var type = fe.GetType();
        return (from propertyname in propertyNamesToCheck
                select type.GetPublicStaticField(propertyname)
                    into field
                    where field != null
                    select field.GetFieldValue<DependencyProperty>(fe))
            .FirstOrDefault();
    }

    public static FieldInfo GetPublicStaticField(this Type type, string fieldName)
    {
        return type.GetField(fieldName, BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12776370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档