0

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

到目前为止我做了什么:

<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;
    }
}

此代码成功地过滤了group1我元素中包含文本的所有元素contact_grname

但是如何绑定到我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;
}
4

2 回答 2

0

一旦你在你ComboBox的.FilterComboBox

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

yourDataGrid.Items.Refresh();.

和 CollectionView :

yourCollectionView.Refresh();

此外,请查看这篇文章,解释CollectionView.

于 2012-12-27T14:40:03.700 回答
0

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

    <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>

结果: 在此处输入图像描述 在此处输入图像描述

于 2012-12-27T10:51:23.457 回答