WPF 过滤集合
<Window x:Class="ViewExam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox Name="cmbProducts" DisplayMemberPath="ModelName" Text="{Binding Path=ModelName}"
SelectionChanged="cmbProducts_SelectionChanged_1"></ComboBox>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock>Model Number</TextBlock>
<TextBox Text="{Binding Path=ModelNumber}" Grid.Column="1"></TextBox>
<TextBlock Grid.Row="1">Model Name</TextBlock>
<TextBox Text="{Binding Path=ModelName}" Grid.Column="1" Grid.Row="1"></TextBox>
<TextBlock Grid.Row="2">Unit Cost</TextBlock>
<TextBox Text="{Binding Path=UnitCost}" Grid.Column="1" Grid.Row="2"></TextBox>
<TextBlock Grid.Row="3">Description</TextBlock>
<TextBox Text="{Binding Path=Description}" TextWrapping="Wrap" Grid.Row="5" Grid.ColumnSpan="2"></TextBox>
</Grid>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Button Name="btnPrevious" Click="btnPrevious_Click_1">previous</Button>
<Label x:Name="lblPosition" Width="400"></Label>
<Button Name="btnNext" Click="btnNext_Click_1">Next</Button>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<Label>Price than</Label>
<TextBox Name="txtMin" Width="200"></TextBox>
<Button Name="btnFilter" Click="btnFilter_Click_1">Filter</Button>
<Button Name="btnRemoveFilter" Margin="3,0,0,0" Click="btnRemoveFilter_Click_1">Remove Filter</Button>
</StackPanel>
</Grid>
</Window>
using DBAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ViewExam
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private ListCollectionView view;
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
ICollection<Product> products = StoreDB.GetProducts();
cmbProducts.ItemsSource = products;
this.DataContext = products;
view = (ListCollectionView)CollectionViewSource.GetDefaultView(products);
//view.Filter = new Predicate<object>(FilterProduct);
view.CurrentChanged += view_CurrentChanged;
view_CurrentChanged(this, null);
}
private bool FilterProduct(object obj)
{
Product pro = (Product)obj;
return pro.UnitCost > 100;
}
void view_CurrentChanged(object sender, EventArgs e)
{
lblPosition.Content = "Record " + (view.CurrentPosition + 1).ToString() + " of " + view.Count.ToString();
btnPrevious.IsEnabled = view.CurrentPosition > 0;
btnNext.IsEnabled = view.CurrentPosition < view.Count - 1;
}
private void btnPrevious_Click_1(object sender, RoutedEventArgs e)
{
view.MoveCurrentToPrevious();
}
private void btnNext_Click_1(object sender, RoutedEventArgs e)
{
view.MoveCurrentToNext();
}
private void cmbProducts_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
view.MoveCurrentTo(cmbProducts.SelectedItem);
}
ProductByPriceFilter filter;
private void btnFilter_Click_1(object sender, RoutedEventArgs e)
{
decimal min = Convert.ToDecimal(txtMin.Text);
filter = new ProductByPriceFilter(min);
view.Filter = filter.FilterItem;
}
private void btnRemoveFilter_Click_1(object sender, RoutedEventArgs e)
{
view.Filter = null;
}
}
}
using DBAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ViewExam
{
public class ProductByPriceFilter
{
public decimal MinimumPrice { get; set; }
public ProductByPriceFilter(decimal minimumPrice)
{
MinimumPrice = minimumPrice;
}
public bool FilterItem(object item)
{
Product pro = (Product)item;
if (pro!=null)
{
return pro.UnitCost > MinimumPrice;
}
return false;
}
}
}
WPF 过滤集合的更多相关文章
- 使用filter方法过滤集合元素
文章转自https://my.oschina.net/nenusoul/blog/658238 Problem 你想要筛选出集合中的一些元素形成一个新的集合,这些元素都是满足你的筛选条件的. Solu ...
- laravel的filter()方法的使用 (方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容)
filter 方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容: $collection = collect([1, 2, 3, 4]); $filtered = $collec ...
- WPF 支持集合绑定的控件
WPF 支持集合绑定的控件 ListBox ComboBox ListView DataGrid
- 使用Java Stream,提取集合中的某一列/按条件过滤集合/求和/最大值/最小值/平均值
不得不说,使用Java Stream操作集合实在是太好用了,不过最近在观察生产环境错误日志时,发现偶尔会出现以下2个异常: java.lang.NullPointerException java.ut ...
- 使用Guava提供的filter过滤集合
正常情况下,我们声明一个List需要如下代码 List<String> list = new ArrayList<>(); list.add("AAA"); ...
- java8 按条件过滤集合
//黄色部分为过滤条件list.stream().filter(user-> user.getId() > 5 && "1组".equals(user. ...
- asp.net通过distinct过滤集合(list)中重复项的办法
/// <summary> /// 权限Distinct比较器 /// </summary> public class PermissionIdComparer : IEqua ...
- WPF 绑定集合 根据集合个数改变样式 INotifyCollectionChanged
问题:当前ListBox Items 绑定 集合数据源ListA时候:ListA集合数据源中存在另外一个集合ListB,当更改或往ListB集合中添加数据的时候,通知改变? 实体类继承 INotify ...
- java8的stream系列教程之filter过滤集合的一些属性
贴代码 List<Student> lists = new ArrayList<>(); Student student = new Student(); student.se ...
随机推荐
- php实现找两个链表的第一个公共结点(实例演示)
php实现找两个链表的第一个公共结点(实例演示) 一.总结 因为是链表,第一个节点公共之后,后面所有的节点都公共了 画个图实例演示一下,会超清晰且简单 二.php实现找两个链表的第一个公共结点 题目描 ...
- 【codeforces 742A】Arpa’s hard exam and Mehrdad’s naive cheat
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Java多线程系列-线程创建
1.怎样创建多线程? Java从语言级别实现多线程,因此实现一个多线程程序很easy.有两种方法能够实现多线程,即继承Thread类和实现Runnable接口.由于Java不支持多继承的原因,建议尽可 ...
- php面试题11(边看边复习刚刚讲的)(array_multisort($arr1,$arr2); 用$arr1来排序$arr2。)
php面试题11(边看边复习刚刚讲的)(array_multisort($arr1,$arr2); 用$arr1来排序$arr2.) 一.总结 1.边看边复习刚刚讲的 2.array_multisor ...
- [Angular Directive] Create a Template Storage Service in Angular 2
You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. Y ...
- kali 系统的源
sources.list deb http://http.kali.org/kali kali-rolling main non-free contrib deb http://mirrors.ust ...
- 与Qt的联系方式:邮件,论坛,销售,Bug报告
If you want to learn more about upcoming things for Qt, please stay tuned for new blog posts and web ...
- POJ 3714 Raid(平面近期点对)
解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...
- hello.c内核模块编译 -- linux内核
Linux开发模块,在本机上看调试信息的方法走通了.当前版本号2.6.32-32-generic uname –r 能够查询 这里取module_param()作为样例. 该宏被定义在include/ ...
- 【t029】Mobile Service
Time Limit: 3 second Memory Limit: 256 MB [问题描述] 一个公司有三个移动服务员.如果某个地方有一个请求,某个员工必须赶到那个地方去(那个地方没有其他员工), ...