WPF filter with ComboBox

╄→гoц情女王★ 提交于 2019-12-13 02:43:31

问题


I'm new to WPF and I want to filter some data with CollectionView with my ComboBox control.

What I have done so far:

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

This code successfully filters all elements with the group1 text within my contact_grname element.

But how to bind to my ComboBox which contains all contact_grnames (XML binded) ?!

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

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

回答1:


If I understand you correctly you want to bind another combobox to the items that are in the group of the first combobox.

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

Result:




回答2:


Once you select an item in your ComboBox, filter the elements you want to show, according to the item selected, calling the Filter method, passing to it the value selected in the ComboBox.

Then, refresh the datagrid, with:

yourDataGrid.Items.Refresh();.

and the CollectionView with:

yourCollectionView.Refresh();

Furthermore, have a look to the this article, explaining the features of a CollectionView.



来源:https://stackoverflow.com/questions/14052468/wpf-filter-with-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!