【问题标题】:wpf ObservableCollection fill comboboxwpf ObservableCollection 填充组合框
【发布时间】:2013-01-03 11:42:22
【问题描述】:

如何将我的组合框填充到我的 ObservableCollectio 项目中?

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

联系人中的项目是“Grname”。这些项目需要绑定到它。首选代码,因为我想过滤掉重复项(分组)。

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

}

更新:

我找到了!

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

cmbGroup.ItemsSource = contactsView.Groups;

但是如何使用组合框的选定项过滤我的数据网格?

我有:

    void Filter(object sender, FilterEventArgs e)
    {

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

并且过滤器绑定在我的 XAML 中的 CollectionViewSource 中

【问题讨论】:

    标签: wpf combobox observablecollection


    【解决方案1】:

    对于过滤、分组、排序等,您可以使用CollectionViewSource。这意味着类似

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

    然后你将你的组合框绑定到 contactsView。

    【讨论】:

    • @keno 如果您可以更具体地说明您尝试过的内容或无效的内容,也许我可以详细说明一下我的示例。
    • 感谢您的浏览!无论如何我找到了解决方案;)但是如何过滤我的网格? (见主题)
    猜你喜欢
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2020-10-28
    • 2013-06-22
    • 2011-11-20
    • 2011-11-02
    相关资源
    最近更新 更多