从ItemsControl的项目绑定到ItemControl的DataContext的 [英] Binding from items in ItemsControl to ItemControl's DataContext
问题描述
我有一个自定义的ItemsTemplateSelector组合框。为控制这些项目的XAML定义,像这样:
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>
第三项目,我想绑定到组合框的的DataContext TheValue属性的值属性。该绑定失败,出现以下错误:
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:
找不到理事FrameworkElement的或FrameworkContentElement上的目标元素BindingEx pression:路径= TheValue;的DataItem = NULL;目标元素是'MyItemWithValue(哈希code = 49465727);目标属性是值 (类型'的Int32')
"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')"
我想这是监守项集合不使用组合框的DataContext的。我曾尝试没有成功的RelativeSource不同的排列,所以我的问题是:什么是完成最好的方式结合
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?
编辑:
RV1987回答我的问题,因为它指出。不过,我想是的结合是双向的,也不提出的解决方案,似乎为这方面的工作。麻烦的可能是,我不能在代理是双向绑定;编译器拒绝接受
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 ={结合,模式=双向}
DataContext="{Binding, Mode=TwoWay}"
推荐答案
ComboboxItems
所以它们没有连接到的数据上下文不可视树的一部分组合框
。你必须使用代理绑定提及到DataContext。有关详细的清洁方法,请看看这个链接 -
ComboboxItems
are not part of visual tree so they are not connected to the data context of the Combobox
. You have to use the proxy binding to refer to the dataContext. For detailed and clean approach please have a look at this link -
<一个href=\"http://tomlev2.word$p$pss.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/\" rel=\"nofollow\">http://tomlev2.word$p$pss.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
另外,看这个(同样的问题,但在这种情况下,它代替组合框的数据网格)由AngelWPF建议,这也是新的东西,我 -
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 -
编辑 - 此外,你需要设置结合模式双向您comboboxitem而不是静态资源设置它的。这应该工作 -
Edit- Moreover, you need to set binding mode Two way in your comboboxitem instead of setting it in StaticResource. This should work -
<local:MyItemWithValue Heading="Third" Text="Third Item" Value="{Binding TheValue, Mode=TwoWay}" />
这篇关于从ItemsControl的项目绑定到ItemControl的DataContext的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!