如何解决从bindingexpression绑定的对象与WPF? [英] How to resolve bound object from bindingexpression with WPF?

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

问题描述

喜没有人知道是否有任何内置类解决从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?

我试图写一个对于混合文本框自动调用绑定到文本框的文本属性的对象上的方法3的行为。

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

文本框绑定到一个ViewModel类的属性。 。我想要做的就是从绑定表达式解析视图模型类,然后让这个电话

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文档一圈又一圈与反射器组件反弹,都无济于事 - 我无法找到必须存在一定什么 - 我们有了一些类执行路径为的DataItem的分辨率(数据上下文)。

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;
}

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

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