你如何添加一个通用的项目绑定到WPF集合的组合框 [英] How do you add a generic item to a ComboBox bound to a collection in WPF

查看:145
本文介绍了你如何添加一个通用的项目绑定到WPF集合的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到Department对象的在C#ViewModel类一个ObservableCollection的WPF应用程序组合框。我想使用组合框按部门筛选另一个集合(实际上它现在的作品为)的问题是,我想增加一个额外的选项全部到列表的顶部。是否有这样做的正确道路。制作一个假的部门感到不对劲在很多方面。



组合框

 <组合框的ItemsSource ={绑定路径=部门}
的SelectedValue ={绑定路径= DepartmentToShow,模式=双向}/>


解决方案

您可以使用的CompositeCollection 作为组合框的ItemsSource将包括全部选项。你需要将CollectionContainer的收藏属性设置为处对象的ObservableCollection

 <组合框> 
< ComboBox.ItemsSource>
< CompositeCollection>
< ComboBoxItem>所有< / ComboBoxItem>
< CollectionContainer X:NAME =departmentCollection/>
< / CompositeCollection>
< /ComboBox.ItemsSource>
< /组合框>



不知道这是否是适合你的过滤情况然而...


I have a ComboBox in a WPF application that is bound to an ObservableCollection of Department objects in a C# ViewModel class. I want to use the combo box to filter another collection by department (And indeed it works for that now) The problem is that I want to add an additional option "All" to the top of the list. Is there a correct way to do this. Making a fake department feels wrong in so many ways.

The ComboBox

<ComboBox ItemsSource="{Binding Path=Departments}" 
          SelectedValue="{Binding Path=DepartmentToShow , Mode=TwoWay}" />

解决方案

You could use a CompositeCollection as the ItemsSource for the ComboBox to include the "All" option. You need to set the Collection property of the CollectionContainer to your "ObservableCollection of Department objects".

<ComboBox >
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ComboBoxItem>All</ComboBoxItem>
            <CollectionContainer x:Name="departmentCollection"/>
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

Not sure if this will be suitable for your filtering situation however...

这篇关于你如何添加一个通用的项目绑定到WPF集合的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆