问题描述
我刚刚接触WPF,我想用 CollectionView
和我的 ComboBox
控制过滤一些数据。 / p>
我到目前为止做了什么:
< CollectionViewSource x:Key =TeleViewSource ={StaticResource TeleData}Filter =Filter>
< CollectionViewSource.SortDescriptions>
< scm:SortDescription PropertyName =contact_nameDirection =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)
{
.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;
}
}
此代码成功过滤所有元素, $ c>
$ b> $ <$ p> $ b>包含所有 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;
}
解决方案您的 ComboBox
,根据所选项目过滤要显示的元素,调用过滤器
它是在 ComboBox
中选择的值。
然后,刷新datagrid:
yourDataGrid.Items.Refresh );
。
和CollectionView:
yourCollectionView.Refresh();
此外,请查看文章,解释 CollectionView
的功能。
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;
}
解决方案 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
.
这篇关于WPF过滤器与ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!