如何使用 WPF 从 bindingexpression 解析绑定对象? [英] How to resolve bound object from bindingexpression with WPF?

查看:14
本文介绍了如何使用 WPF 从 bindingexpression 解析绑定对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道是否有任何内置类用于从 bindingexpression 解析绑定对象及其 DataItem 和属性路径?

Hi does anyone know if there are any inbuilt classes for resolving a bound object from a bindingexpression and it's DataItem and property path?

我正在尝试为文本框编写 Blend 3 行为,该行为会自动调用绑定到文本框 Text 属性的对象上的方法.

I'm attempting to write a Blend 3 behavior for textboxes which automatically invokes methods on an object bound to the textbox Text property.

文本框绑定到视图模型类上的属性.我想要做的是从绑定表达式解析视图模型类,然后调用它.

The textbox is bound to a property on a viewmodel class. What I want to do is resolve the viewmodel class from the binding expression and then make calls on this.

我首先从行为的关联对象中检索绑定表达式,如下所示:

I first retrieve the binding expression from the behavior's associated object like so:

private BindingExpression GetTextBinding()
{
    return this.AssociatedObject.GetBindingExpression(TextBox.TextProperty);
}

完成此操作后,如果我们查看绑定表达式,我们可以看到它通过绑定表达式的 DataItem 属性引用了数据上下文.

Having done this, if we look at the binding expression, we can see it has a reference to the data context via the binding expression's DataItem property.

此外,我们还有绑定在绑定表达式的父绑定上的属性的相对路径.

In addition, we have the relative path of the property which is bound on the binding expression's parent binding.

所以,我们可以得到这些信息:

So, we can get this information:

var bindingExpression = GetTextBinding();
object dataContextItem = bindingExpression.DataItem;
PropertyPath relativePropertyPath = bindingExpression.ParentBinding.Path;

现在,这个属性路径可能是一个深度嵌套和复杂的路径,我非常希望避免(重新?)实现解析.我搜索了 .NET 文档并使用反射器在程序集周围进行了搜索,但都无济于事 - 我找不到肯定存在的东西 - 必须有一些类来执行数据项路径的解析(数据上下文).

Now, this property path could potentially be a deeply nested and complex path, which I would very much like to avoid having to (re?)implement resolution of. I've searched around the .NET documentation and bounced around the assemblies with reflector, all to no avail - I can't find what surely must exist - there's got to be some class which performs the resolution of the path for the dataitem (the data context).

有谁知道这可能存在于何处?对解决绑定对象的替代方法有什么建议吗?

Does anyone know where this might exist? Any suggestions for alternative ways of resolving the bound object?

注意,我正在尝试获取绑定对象,它是绑定属性的(在本例中为字符串) - 我显然可以轻松获取绑定值,但它是我需要的父母.

Note, I'm trying to get at the bound object which is the parent of the bound property (the string in this case) - I can obviously easily get at the bound value, but it's the parent I need.

在此先感谢您的帮助!菲尔

Thanks in advance for any help! Phil

推荐答案

下面是一个扩展方法的快速实现,它可以满足您的需求.我也找不到与此相关的任何内容.如果由于某种原因找不到值,下面的方法将始终返回 null.当路径包含 [] 时,该方法将不起作用.我希望这会有所帮助!

Below is a quick implementation for an extension method that will do just what you are looking for. I couldn't find anything related to this either. The method below will always return null if for some reason the value cannot be found. The method won't work when the path includes []. I hope this helps!

public static T GetValue<T>(this BindingExpression expression, object dataItem)            
{
    if (expression == null || dataItem == null)
    {
        return default(T);
    }

    string bindingPath = expression.ParentBinding.Path.Path;
    string[] properties = bindingPath.Split('.');

    object currentObject = dataItem;
    Type currentType = null;

    for (int i = 0; i < properties.Length; i++)
    {
        currentType = currentObject.GetType();                
        PropertyInfo property = currentType.GetProperty(properties[i]);
        if (property == null)
        {                    
            currentObject = null;
            break;
        }
        currentObject = property.GetValue(currentObject, null);
        if (currentObject == null)
        {
            break;
        }
    }

    return (T)currentObject;
}

这篇关于如何使用 WPF 从 bindingexpression 解析绑定对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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