2015-01-26 37 views
2

我有一个wpf组合框绑定到我的viewmodel中的IEnumberable集合。当组合框第一次绑定时,选择一个空值。在组合框中选择任何其他值时,空值将消失。有没有办法让空值保留而不改变集合?WPF comobox绑定空值

<ComboBox ItemsSource="{Binding CarCollection}" SelectedItem="{Binding SelectedCar}" 
     <ComboBox.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding CarName}" VerticalAlignment="Center"         
       </DataTemplate> 
      </ComboBox.ItemTemplate> 
</ComboBox> 

回答

2

不,你不能有null是一种选择,除非它实际上是在列表中。

你当然可以将支持属性设置为null这应该清除UI选择。如果您需要需要 null属性,而不修改视图模型中的列表,请考虑使用CompositeCollection。有了它,你可以做这样的事情:

<CollectionViewSource x:Key="ComboBoxItems"> 
    <CompositeCollection> 
     <ListViewItem>Pick a choice</ListViewItem> 
     <CollectionContainer Source="{Binding MyCollection}"/> 
    </CompositeCollection> 
</CollectionViewSource> 

一个完整的例子可以在MSDN找到。