Binding from items in ItemsControl to ItemControl's DataContext

China☆狼群 提交于 2019-12-13 00:28:51

问题


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


回答1:


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 -

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 Heading="Third" Text="Third Item" Value="{Binding TheValue, Mode=TwoWay}" />



回答2:


local:MyItemWithValue is not a FrameworkElement, so it can't inherit the ComboBox DataContext.
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."




回答3:


I would have thought the quickest solution was simply to bind to the ComboBoxs DataContext property. You should be able to get around the problems with RelativeSource by using a named element:

<ComboBox x:Name="combo" 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 DataContext.TheValue, ElementName=combo}" />
</ComboBox>


来源:https://stackoverflow.com/questions/7711897/binding-from-items-in-itemscontrol-to-itemcontrols-datacontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!