I need a ComboBox with a non-selected constant element, which means that SelectedValue is null .

At the same time data sources can be of various types. Whether it is List<MyClass> or List<int> .

So far, nothing more or less rational comes to mind. It’s unreasonable to create new collections from existing ones with the first null value and use them, but something tells me that this is crap.

  • And what do you want the value to be when you select this "Not Selected"? - Andrey NOP
  • Shl. null in the List <> not add, sort of ... - Andrey NOP
  • @ Andrey, I don’t need to add null to the list. The main thing here is that the type of property to which I will bind SelectedValue accept null - iRumba

1 answer 1

You can do this:

1) Create a collection in resources that is tied to your real data:

 <Window.Resources> <CollectionViewSource x:Key="MyKey" Source="{Binding Path=MyData}" /> </Window.Resources> 

2) Next, in the ComboBox as a ItemsSource use a CompositeCollection , it looks like this:

 <ComboBox> <ComboBox.ItemsSource> <CompositeCollection> <ComboBoxItem Content="Не выбрано" /> <CollectionContainer Collection="{Binding Source={StaticResource MyKey}}" /> </CompositeCollection> </ComboBox.ItemsSource> </ComboBox> 
  • What in your case will appear in SelectedValue ? - Andrey NOP
  • Is it possible to universalize it? And then for each ComboBox you have to create such a collection. - iRumba
  • @ Andrei there will be null - iRumba
  • @iRumba, you can look in the direction of Attached Property , Behavior or your control, in which the code implementing this functionality is rendered. - sp7