CollectionContainer doesn't bind my Collection

女生的网名这么多〃 提交于 2020-01-06 21:12:08

问题


I have something like a form where the user can assign a cutomer to a vehicle. But this is optional. The customers are in a combobox. I need a way to make sure that the user can choose 'none' if the combobox is open.

if have found something like this:

<ComboBox.ItemsSource>    
    <CompositeCollection>
       <ComboBoxItem IsEnabled="False" Foreground="Black">none</ComboBoxItem>
       <CollectionContainer Collection="{Binding Source={StaticResource DataKey}}" />    
    </CompositeCollection>
</ComboBox.ItemsSource>

But the Binding to the Collection doesn't work for me.

My form is an userControl which is included in a WPF-Window. The DataContext is on a Grid in the Window. Therefore, I have tried:

<CollectionContainer Collection="{Binding Customers, Source={RelativeSource AncestorType=Grid}}" />

But the comboBox only shows 'none'. How can I solve my problem?

Thanks in advance!


回答1:


Assuming you took the code here: ComboBox with empty item?

The answer says that the binding won't work, as it has no access. Use BindingProxy if you want to fix it: http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/



来源:https://stackoverflow.com/questions/27845386/collectioncontainer-doesnt-bind-my-collection

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