C#WPF ComboBox过滤ListView

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

各位大家好,我正在做一个项目,我在列表视图中列出了保存在文本文件中的项目,但现在我有一个问题我想把一个组合框放在哪里我可以选择两个选项和过滤列表视图。我怎样才能做到这一点?我有一个过滤器,但不能正常工作每次我从组合框中选择两个项目中的一个他告诉我另一个在listView中有像切换。这是我的代码谢谢: private readonly ObservableCollection< Chave> chaves = new ObservableCollection< Chave>(); public ConsultarChaves() { InitializeComponent(); } private void button_voltar_Click(object sender,RoutedEventArgs e) { MainWindow sw = new MainWindow(); sw.Show(); this.Close(); } private void button_consultar_chaves_Click(object sender,RoutedEventArgs e) { string [] lines = File.ReadAllLines(GerarChave.FilePath); foreach(行中的换行符) { string [] parts = line.Split(','); Chave a = new Chave(); a.numero1 = parts [0]; a.numero2 = parts [1]; a.numero3 =部分[2]; a.numero4 = parts [3]; a.numero5 = parts [4]; a.estrela1 =部分[5]; a.estrela2 =部分[6]; a.DiaDaSemana =部分[7]; a.Data = parts [8]; chaves.Add(a); }¥b $ b listView.ItemsSource = chaves; } private bool myTextFilter(object item) { if(String.IsNullOrEmpty(comboBox_filtro.Text)) 返回true; var chaves =(Chave)item; return(chaves.DiaDaSemana.StartsWith(comboBox_filtro.Text)); $ } private void comboBox_filtro_SelectionChanged(object sender,SelectionChangedEventArgs e) { $ ICollectionView view = CollectionViewSource.GetDefaultView(listView.ItemsSource); view.Filter = myTextFilter; }¥b $ b }

Hello everyone, i'm doing a project where i list items in a listview that are saved in a text file, but now i have a problem i want to put a combobox where i can select between two option and filter the listview. How can i do this? i have do a filter but is not working properly every time i select one of the two items from the combobox he show me the other one in the listView there are like switched. Here is my code thanks: private readonly ObservableCollection<Chave> chaves = new ObservableCollection<Chave>(); public ConsultarChaves() { InitializeComponent(); } private void button_voltar_Click(object sender, RoutedEventArgs e) { MainWindow sw = new MainWindow(); sw.Show(); this.Close(); } private void button_consultar_chaves_Click(object sender, RoutedEventArgs e) { string[] lines = File.ReadAllLines(GerarChave.FilePath); foreach(string line in lines) { string[] parts = line.Split(','); Chave a = new Chave(); a.numero1 = parts[0]; a.numero2 = parts[1]; a.numero3 = parts[2]; a.numero4 = parts[3]; a.numero5 = parts[4]; a.estrela1 = parts[5]; a.estrela2 = parts[6]; a.DiaDaSemana = parts[7]; a.Data = parts[8]; chaves.Add(a); } listView.ItemsSource = chaves; } private bool myTextFilter(object item) { if (String.IsNullOrEmpty(comboBox_filtro.Text)) return true; var chaves = (Chave)item; return (chaves.DiaDaSemana.StartsWith(comboBox_filtro.Text)); } private void comboBox_filtro_SelectionChanged(object sender, SelectionChangedEventArgs e) { ICollectionView view = CollectionViewSource.GetDefaultView(listView.ItemsSource); view.Filter = myTextFilter; } }

-------------------------------------

-------------------------------------

XAML代码

推荐答案

您好,

欢迎来到MSDN论坛。

Welcome to MSDN forum.

这个论坛是关于visual studio IDE的,我将把你的主题转移到WPF论坛寻求专业支持,谢谢理解。

This forum is to talk about visual studio IDE, I will move your thread to WPF forum for professional support, thank you for understanding.

祝你好运,

Joyce

更多推荐

C#WPF ComboBox过滤ListView

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

发布评论

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

>www.elefans.com

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