简体   繁体   English

wpf ObservableCollection填充组合框

[英]wpf ObservableCollection fill combobox

How do I fill up my combobox to an item of my ObservableCollectio ? 如何将组合框填充到ObservableCollectio的项目中?

public ObservableCollection<Contacts> contacts = new ObservableCollection<Contacts>();

Item within Contacts is "Grname". 联系人中的项目为“姓氏”。 Those items need to be binded to it. 这些项目需要绑定到它。 Prefer by code, because I want to filter out the duplicates (grouping). 首选代码,因为我想过滤出重复项(分组)。

    class Contacts
{
    public string Contact_id { get; set; }
    public string Grname { get; set; }

}

UPDATE: 更新:

I found it ! 我找到了 !

ICollectionView contactsView = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);

cmbGroup.ItemsSource = contactsView.Groups;

But how to filter my datagrid with the selected item of combobox ? 但是如何使用组合框的选定项过滤我的datagrid?

I've got: 我有:

    void Filter(object sender, FilterEventArgs e)
    {

        if (cmbGroup.ItemsSource == contactsView)
        {
            e.Accepted = true;
        }
        else
        {
    e.Accepted = false;
    }
}

And Filter is binded in CollectionViewSource in my XAML 并且筛选器绑定到我的XAML中的CollectionViewSource中

For filtering, grouping, sorting etc. you could use a CollectionViewSource . 为了进行过滤,分组,排序等,您可以使用CollectionViewSource That means something like 那意味着

ICollectionView contactsView = CollectionViewSource.GetDefaultView(contacts);
// For filtering:
contactsView.Filter += sender => {
    // Filter logic here
    return true;
}

Then you bind your combobox against the contactsView. 然后,将您的组合框绑定到contactsView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM