• wpf,CollectionViewSource,使用数据过滤 筛选 功能。


        class TextListBoxVMpublic : ViewModelBase
    	{
            public TextListBoxVMpublic()
    		{
    			var list = this.GetEmployees();
    			this.filteredEmploees = new CollectionViewSource();
    			this.filteredEmploees.Source = list;
    			this.filteredEmploees.Filter += this.EmployeeFilter;
    		}
    
    	
    
    		private CollectionViewSource filteredEmploees;
    		public CollectionViewSource FilteredEmploees
    		{
    			get { return filteredEmploees; }
    			set { filteredEmploees = value; }
    		}
    
    		private string filterText = string.Empty;
    		public string FilterText
    		{
    			get { return filterText; }
    			set
    			{
    				filterText = value;
                    Notify("FilterText");
    				this.FilteredEmploees.View.Refresh();
    			}
    		}
    
    
    		private void EmployeeFilter(object sender, FilterEventArgs e)
    		{
    			e.Accepted = this.EmployeeFilter(e.Item);
    		}
    
    		private bool EmployeeFilter(object item)
    		{
    			var employee = item as Employee;
    			return employee != null && employee.Name.ToLower().Contains(this.FilterText.ToLower());
    		}
    
    		private ObservableCollection<Employee> GetEmployees()
    		{
    			var list = new ObservableCollection<Employee>();
    
    			list.Add(new Employee { Name = "Huon Laprice", Position = "Events Assistant", Team = "Marketing", Office = "USA Office : Boston, MA", Address = "321 Jones Rd , Waltham, MA 04462", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image51.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Keiran Hughes", Position = "Trainee", Team = "Marketing", Office = "Canada Office: Toronto", Address = "110-556 Portage Ave. Winnipeg, R3B 5A7", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image52.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Swen Trommler", Position = "Brand Manager", Team = "Marketing", Office = "United Kingdom Office: London", Address = "15 Bedford Square London WC81C 15JA", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image53.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Valentino Lorenco", Position = "Brand Manager", Team = "Marketing", Office = "Canada Office: Toronto", Address = "110-556 Portage Ave. Winnipeg, R3B 5A7", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image55.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Linda Rodriges", Position = "Customer Advocate", Team = "Marketing", Office = "USA Office : San Diego, CA", Address = "12887 Scripps Summit Ct.", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image54.png", UriKind.RelativeOrAbsolute) });
    
    
    			list.Add(new Employee { Name = "Elizabeth Brow", Position = "Events Assistant", Team = "Sales", Office = "USA Office : Austin, TX", Address = "Brazos Street, Suite 400", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image56.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Deena Greece", Position = "Trainee", Team = "Sales", Office = "USA Office : Austin, TX", Address = "Brazos Street, Suite 400", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image57.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Janine Labrune", Position = "Events Assistant", Team = "Sales", Office = "Canada Office: Toronto", Address = "Toronto 110-556 Portage Ave. Winnipeg", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image58.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "John Steel", Position = "Events Assistant", Team = "Sales", Office = "Canada Office: Toronto", Address = "Toronto 110-556 Portage Ave. Winnipeg", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image59.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Jaime Yorres", Position = "Trainee", Team = "Sales", Office = "USA Office : Austin, TX", Address = "510 Brazos Street, Suite 400", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image60.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Aria Cruz", Position = "Trainee", Team = "Sales", Office = "San Diego, CA", Address = "12887 Scripps Summit Ct.", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image61.png", UriKind.RelativeOrAbsolute) });
    
    
    
    			list.Add(new Employee { Name = "Peter Franken", Position = "Trainee", Team = "Finance", Office = "USA Office : San Diego, CA", Address = "12887 Scripps Summit Ct.", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image62.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Simon Crowther", Position = "Trainee", Team = "Finance", Office = "United Kingdom Office: London", Address = "15 Bedford Square London WC81C 15JA", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image63.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "John Terry", Position = "Brand Manager", Team = "Finance", Office = "United Kingdom Office: London", Address = "15 Bedford Square London WC81C 15JA", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image64.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Michael Holz", Position = "Brand Manager", Team = "Finance", Office = "United Kingdom Office: London", Address = "110-556 Portage Ave. Winnipeg, R3B 5A7", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image65.png", UriKind.RelativeOrAbsolute) });
    			list.Add(new Employee { Name = "Georg Pipps", Position = "Brand Manager", Team = "Finance", Office = "Canada Office: Toronto", Address = "15 Bedford Square London WC81C 15JA", PhotoPath = new Uri("../../Images/ListBox/GroupingFiltering/Image66.png", UriKind.RelativeOrAbsolute) });
    			
    			return list;
    		}
    	}
        public class Employee
        {
            public string Name { get; set; }
            public string Position { get; set; }
            public string Team { get; set; }
            public string Office { get; set; }
            public string Address { get; set; }
            public Uri PhotoPath { get; set; }
        }
    }
    

      

    <ListBox ItemTemplate="{DynamicResource DataTemplate1}" ItemsSource="{Binding TheSource2.View}"/>
    

      

    代码很详细,主要使用

    e.Accepted  true 或 false 来控制筛选
  • 相关阅读:
    C# 计算结果四舍五入
    同时执行多条SQL语句
    将一个datatable的值赋给另一个dt的三种方法转
    Oracle中增加、删除、修改字段
    Oracle 某列转为行
    DataList中链接跳转页面传参数
    修改数据库中多个表中的同一个字段的长度(可参照修改成同一字段的列名、注释等)
    克隆表结构
    博客专题
    Rational Rose2003 安装教程
  • 原文地址:https://www.cnblogs.com/m7777/p/4595717.html
Copyright © 2020-2023  润新知