从 ItemsControl 中的项目绑定到 ItemControl 的 DataContext
我有一个带有自定义 ItemsTemplateSelector 的 ComboBox。该控件的项目在 xaml 中定义,如下所示:
<ComboBox ItemTemplateSelector="{StaticResource CommonListSelectorTemplates}" >
<local:MyItem Heading="First" Text="First Item"/>
<local:MyItem Heading="Second" Text="Second Item"/>
<local:MyItemWithValue Heading="Third" Text="Third Item" Value="{Binding TheValue}" />
</ComboBox>
第三个项目有一个 Value 属性,我希望将其绑定到 ComboBox 的 DataContext 上的 TheValue 属性。此绑定失败并出现以下错误:
“找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。BindingExpression:Path=TheValue; DataItem=null;目标元素为“MyItemWithValue”(HashCode=49465727);目标属性为“Value”(类型“ Int32')"
我猜这是因为 Items 集合不使用 ComboBox 的 DataContext。我尝试了relativesource的不同排列但没有成功,所以我的问题是:完成绑定的最佳方法是什么?
编辑:
RV1987 回答了我的问题。然而,我想要的是双向绑定,并且所提出的解决方案似乎都不适用于此。问题可能是我无法将代理中的绑定设为双向;编译器拒绝接受
DataContext="{Binding, Mode=TwoWay}"
I have a ComboBox with a custom ItemsTemplateSelector. The Items for the control are defined in xaml, like so:
<ComboBox ItemTemplateSelector="{StaticResource CommonListSelectorTemplates}" >
<local:MyItem Heading="First" Text="First Item"/>
<local:MyItem Heading="Second" Text="Second Item"/>
<local:MyItemWithValue Heading="Third" Text="Third Item" Value="{Binding TheValue}" />
</ComboBox>
The third item has a Value property that I wish to bind to the TheValue property on the ComboBox's DataContext. This binding fails with the following error:
"Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=TheValue; DataItem=null; target element is 'MyItemWithValue' (HashCode=49465727); target property is 'Value' (type 'Int32')"
I guess it's becuase the Items collection does not use the ComboBox's DataContext. I have tried different permutations of RelativeSource without success, so my question is: What is the best way to accomplish the binding?
EDIT:
RV1987 answered my question as it was stated. However, what I want is for the binding to be two-way, and neither of the solutions proposed seem to work for this. The trouble may be that I can't get the binding in the proxy to be two-way; the compiler refuses to accept
DataContext="{Binding, Mode=TwoWay}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ComboboxItems
不是可视化树的一部分,因此它们不连接到Combobox
的数据上下文。您必须使用代理绑定来引用 dataContext。有关详细和干净的方法,请查看此链接 -http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
另外,看看在这个问题上(同样的问题,但在本例中它的数据网格代替了组合框),正如 AngelWPF 所建议的,这对我来说也是新的东西 -
绑定数据网格列可见性 MVVM
编辑 - 此外,您需要在组合框项中设置绑定模式两种方式而不是在 StaticResource 中设置它。这应该有效 -
ComboboxItems
are not part of visual tree so they are not connected to the data context of theCombobox
. You have to use the proxy binding to refer to the dataContext. For detailed and clean approach please have a look at this link -http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
Also, look at this (same issue but in this case its datagrid in place of combobox) as suggested by AngelWPF, this was also something new to me -
Bind datagrid column visibility MVVM
Edit- Moreover, you need to set binding mode Two way in your comboboxitem instead of setting it in StaticResource. This should work -
local:MyItemWithValue
不是 FrameworkElement,因此它无法继承 ComboBoxDataContext
。请参阅此说明:
“WPF 不会在当前版本中为自定义类添加继承上下文,因此第二个绑定无法解析“数据上下文”引用,如果您想启用这种绑定,只需简单地从 FrameworkElement 或 FrameworkContentElement 子类化即可。”
local:MyItemWithValue
is not a FrameworkElement, so it can't inherit the ComboBoxDataContext
.See this note:
"WPF won't add inheritance context for custom classes in current version, so the second binding cannot resolve the "data context" reference, if you wanna enable this kinda binding, just simply subclass from FrameworkElement or FrameworkContentElement."
我本以为最快的解决方案就是简单地绑定到 ComboBox 的 DataContext 属性。您应该能够通过使用命名元素来解决
RelativeSource
的问题:I would have thought the quickest solution was simply to bind to the
ComboBox
sDataContext
property. You should be able to get around the problems withRelativeSource
by using a named element: