2012-12-27 81 views
0

我是WPF的新手,我想用我的ComboBox控件过滤一些数据,CollectionView带组合框的WPF过滤器

我迄今所做的:

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

嘿,所以你想在其他组合框中填充另一个组合框中的项目? –

回答

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> 

结果: enter image description here enter image description here

+0

嗨!感谢您的回复 !没有与另一个组合框:-)我想筛选我的数据网格与我的组合框中选择的项目(contact_grname) – keno

0

一旦您选择了您的ComboBox项,筛选要显示的元素,根据选择的项目,调用Filter方法,并传递到它在ComboBox中选择的值。

然后,刷新数据网格,具有:

yourDataGrid.Items.Refresh();

并用的CollectionView:

yourCollectionView.Refresh(); 

此外,有看向this文章,解释CollectionView的特点。