【问题标题】:WPF filter with ComboBox带有组合框的 WPF 过滤器
【发布时间】:2012-12-27 09:38:43
【问题描述】:

我是 WPF 新手,我想用我的 ComboBox 控件过滤一些带有 CollectionView 的数据。

到目前为止我做了什么:

<CollectionViewSource x:Key="TeleView"  Source="{StaticResource TeleData}" Filter="Filter" >
<CollectionViewSource.SortDescriptions>
    <scm:SortDescription PropertyName="contact_name" Direction="Ascending" />

</CollectionViewSource.SortDescriptions>

<CollectionViewSource.GroupDescriptions>
    <dat:PropertyGroupDescription PropertyName="contact_grname" />

</CollectionViewSource.GroupDescriptions>

CS:

private int count = 0;
void Filter(object sender, FilterEventArgs e)
{

    if (value == "" || value == null)
    {
        e.Accepted = true;
    }
    else
    {

        System.Xml.XmlElement ele = e.Item as System.Xml.XmlElement;
        string name = ele.SelectNodes("/response/contacts/contact/contact_grname")[count].InnerText;
        count += 1;
        //MessageBox.Show(name);

        if (name == "group1") e.Accepted = true;
        else e.Accepted = false;
    }
}

此代码成功过滤了我的contact_grname 元素中带有group1 文本的所有元素。

但是如何绑定到我的ComboBox,其中包含所有contact_grnames(XML 绑定)?!

private void cmbGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    value = cmbGroup.SelectedValue.ToString();
    lblGroupName.Content = "Groupname: " + value;

    CollectionViewSource cvs = FindResource("TeleView") as CollectionViewSource;
}

【问题讨论】:

  • 嘿,你想用另一个组合框中所选组中的项目填充另一个组合框吗?

标签: c# wpf combobox filter


【解决方案1】:

如果我理解正确,您希望将另一个组合框绑定到第一个组合框组中的项目。

    <XmlDataProvider x:Key="TeleData" XPath="/response/contacts/contact" Source="C:\Data.xml" />
    <CollectionViewSource x:Key="TeleView" Source="{StaticResource TeleData}" >
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="contact_name" Direction="Ascending"  />
        </CollectionViewSource.SortDescriptions>
        <CollectionViewSource.GroupDescriptions>
            <dat:PropertyGroupDescription PropertyName="contact_grname"   />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

</Window.Resources>

<StackPanel>
    <ComboBox ItemsSource="{Binding Source={StaticResource TeleView}, Path=Groups}" DisplayMemberPath="Name" Name="comboGroups" />
    <ComboBox ItemsSource="{Binding ElementName=comboGroups, Path=SelectedItem.Items}" DisplayMemberPath="contact_name" Name="comboNames" />
</StackPanel>

结果:

【讨论】:

  • 嗨!谢谢您的回复 !不,不与另一个组合框 :-) 我想用在我的组合框中选择的项目 (contact_grname) 过滤我的数据网格
【解决方案2】:

一旦您在ComboBox 中选择了一个项目,根据所选项目过滤您想要显示的元素,调用Filter 方法,将ComboBox 中选择的值传递给它。

然后,刷新数据网格,使用:

yourDataGrid.Items.Refresh();.

和 CollectionView :

yourCollectionView.Refresh();

此外,请查看this 文章,了解CollectionView 的功能。

【讨论】:

    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2020-04-01
    • 1970-01-01
    相关资源
    最近更新 更多