2012-12-27 107 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的特點。