通过对象绑定到一个对象的属性[属性],而不是object.property [英] Binding to a property of an object through object[property] rather than object.property
问题描述
我已经通过WPF数据读取绑定概述和我的感觉的这应该是可能的,但我不能完全使精神飞跃在这里。我知道,如果我设置了绑定的路径
到富
,并设定来源
到 OBJ
,然后绑定将看看 obj.foo
。换句话说,的obj .__ GETATTR __()
将要访问的。
我要的是让看 OBJ [富]
代替。我使用IronPython和在这种情况下,我的 OBJ
类有一个重写 __的GetItem __()
调用一个成员函数,并返回一个值,这就是为什么我需要这个功能。
下面是编程写入上述例子。这将显示的富
属性 OBJ
在一个复选框:
myBinding =绑定(富)
myBinding.Source = OBJ
acheckbox.SetBinding(CheckBox.IsCheckedProperty,myBinding)
解决方案
这是如此简单,显然!
myBinding =绑定([富])
myBinding.Source = OBJ
acheckbox.SetBinding(CheckBox.IsCheckedProperty,myBinding)
您可以只使用一个绑定,例如如果我的的DataContext
/源有一个属性。
公共字符串这个[INT I]
{
得到{...}
组{...}
}
您可以创建这样的绑定:
< TextBlock的文本={结合[0]}/>
编辑:我觉得我可能误解了你的问题,如果是的话请告诉我
。I've read through the WPF Data Binding Overview and I feel like this should be possible but I can't quite make the mental leap here. I know that if I set a binding's Path
to foo
, and set the Source
to obj
, then the binding will look at obj.foo
. In other words, obj.__getattr__()
will be accessed.
All I want is to make it look at obj[foo]
instead. I'm using Ironpython and in this case my obj
class has an overridden __getitem__()
that calls a member function and returns a value, which is why I need this functionality.
Here is the above example written programmatically. This displays the foo
property of obj
in a checkbox:
myBinding = Binding("foo")
myBinding.Source = obj
acheckbox.SetBinding(CheckBox.IsCheckedProperty, myBinding)
Solution
It's as simple as this, apparently!
myBinding = Binding("[foo]")
myBinding.Source = obj
acheckbox.SetBinding(CheckBox.IsCheckedProperty, myBinding)
You can just use that in a binding, e.g. if my DataContext
/source has a property
public string this[int i]
{
get{...}
set{...}
}
you can create a binding like this:
<TextBlock Text="{Binding [0]}"/>
Edit: I feel like i might have misunderstood your question, if so please tell me.
这篇关于通过对象绑定到一个对象的属性[属性],而不是object.property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!