WPF过滤器与ComboBox

编程入门 行业动态 更新时间:2024-10-10 11:30:40
本文介绍了WPF过滤器与ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚接触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> $

$ 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();

此外,请查看 this 文章,解释 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

本文发布于:2023-11-13 04:39:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   WPF   ComboBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!