问题
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