问题
I need to add an empty item in a bounded combobox within wpf mvvm application, I tried this
<ComboBox TabIndex="23" Text="{Binding Specialisation}" DisplayMemberPath="refsp_libelle">
<ComboBox.ItemsSource>
<CompositeCollection >
<ComboBoxItem > </ComboBoxItem>
<CollectionContainer Collection="{Binding SpecialisationItemSource}" ></CollectionContainer>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
It worked before I tried to add the empty item.
<ComboBox TabIndex="23" Text="{Binding Specialisation}" ItemsSource="{Binding SpecialisationItemSource}" DisplayMemberPath="refsp_libelle"/>
So I need to know :
- What is The error that I made?
- How can I fix it?
thanks,
回答1:
Why does your approach not work?
You use {Binding SpecialisationItemSource}
which, since no source for the binding is explicitly defined, falls back to using target's DataContext
as the source - or rather it would if CollectionContainer
was a FrameworkElement
, and it's not. Hence the source for the binding is null
and no items show up in the combo. You'd need to set the Source
property of the binding explicitly to make it work (setting RelativeSource
or ElementName
wouldn't work either).
Actually even if CollectionContainer
was a FrameworkElement
it still would not work, because CompositeCollection
is not a FrameworkElement
(it's not even a DependencyObject
), so the data context inheritance would be broken).
How to fix it?
In order to use the "implicit binding", you can place a CollectionViewSource
in a resource dictionary, and use that to fill the collection container by using StaticResource
extension:
<ComboBox>
<ComboBox.Resources>
<CollectionViewSource x:Key="Items" Source="{Binding SpecialisationItemSource}" />
</ComboBox.Resources>
<ComboBox.ItemsSource>
<CompositeCollection>
<TextBlock />
<CollectionContainer Collection="{Binding Source={StaticResource Items}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
Notice that I used Collection="{Binding Source={StaticResource Items}}"
instead of just Collection="{StaticResource Items}"
- that's because an object of type CollectionViewSource
is not an actual collection and is not valid value for the CollectionContainer.Collection
property, and binding mechanism is designed to turn it into an actual collection. Also, I replaced an empty ComboBoxItem
with an empty TextBlock
, because the former resulted in binding errors, which I really don't like seeing. Ultimately, I'd even go with replacing it with a default value for the bound collection's item type.
来源:https://stackoverflow.com/questions/33102177/add-an-empty-item-to-a-bounded-combobox