0

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!

GrayFox
  • 997
  • 1
  • 9
  • 26
  • Have you tried binding against `DataContext.Customers` in your second binding? – Erti-Chris Eelmaa Jan 08 '15 at 16:54
  • @ChrisEelmaa Yes, I have tried this: DataContext.Customers, Source={Relative ....} – GrayFox Jan 08 '15 at 16:59
  • use output panel to see if theb binding was successful or not. – Erti-Chris Eelmaa Jan 08 '15 at 17:00
  • @ChrisEelmaa I have some errors but I don't know how to fix this: 'DataContext' property not found on 'object' ''RelativeSource', ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='ComboBoxItem' – GrayFox Jan 08 '15 at 17:35

1 Answers1

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/

Community
  • 1
  • 1
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • 1
    I have found a solution on this page: https://social.msdn.microsoft.com/Forums/vstudio/en-US/cc8fc068-5f3e-4179-be8b-b6b4c50803ff/how-to-use-collectioncontainer-as-itemssource?forum=wpf – GrayFox Jan 08 '15 at 21:26