Add an empty item to a bounded combobox

孤人 提交于 2019-12-12 16:08:14

问题


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 :

  1. What is The error that I made?
  2. 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

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