ComboBox与空项目?

PaN*_*1Me 36 wpf combobox

假设我们有一个DataSource绑定到Database的集合.当然没有空项.如何将void项添加到ComboBox中,以便在第一次加载用户时会看到一个空字符串.我不想在Collection中添加一个dummy/void对象.最适合XAML.有什么建议?

Ars*_*yan 42

<ComboBox Name="myComboBox" Width="200" Background="White">    
    <ComboBox.ItemsSource>    
        <CompositeCollection>
           <ComboBoxItem IsEnabled="False" Foreground="Black">Select Item</ComboBoxItem>
           <CollectionContainer Collection="{Binding Source={StaticResource DataKey}}" />    
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

编辑

正如@surfen在评论中提到的,BindingProxy是绑定问题的解决方法

  • 啊......我明白了.CompositeCollection不是Freezable,因此它不适用于绑定.不幸的. (14认同)
  • 我找到了解决CompositeCollection问题的方法:http://stackoverflow.com/questions/6446699/how-do-you-bind-a-collectioncontainer-to-a-collection-in-a-view-model (4认同)
  • @surfen,因为你的博客已移动这是新的网址:http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not -遗传/ (3认同)
  • 查看已编辑的帖子,您只需将IsEnabled ="False"前景="黑色"添加到项目属性即可 (2认同)
  • 我似乎无法在ViewModel绑定场景中使用它...任何想法? (2认同)
  • 是的,它的确有效!我使用BindingProxy来解决绑定问题:http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ (2认同)