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 ...
随机推荐
- mycat主从读写分离范例
1.mycat二进制包安装 tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gzcd mycatmv mycat /opt/ ...
- css 弹性盒模型Flex 布局
参考文章:http://www.runoob.com/w3cnote/flex-grammar.html Flex 布局是什么:采用Flex布局的元素,称为Flex容器(flex container) ...
- .net core——Docker化开发和部署
原文:.net core--Docker化开发和部署 本篇文章是使用Vs2017生成的Dockerfile进行部署的. 目录 VS2017生成Docker部署项目 Dockerfile内容 在开发服务 ...
- Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'。
Mybatis找不到参数错误:There is no getter for property named 'categoryId' in 'class java.lang.Integer'. 错误Li ...
- 一起学Python:字符串介绍
字符串介绍 <1>python中字符串的格式 如下定义的变量a,存储的是数字类型的值 a = 100 如下定义的变量b,存储的是字符串类型的值 b = "hello itcast ...
- vector, list, deque的选用(vector适用少量对象,list适用大量对象),以及效率问题
如何选择这三个容器中哪一个,应根据你的需要而定,一般应遵循下面的原则: 1.如果你需要高效的随机存取,而不在乎插入和删除的效率,使用vector 2.如果你需要大量的插入和删除,而不关心随机存取( ...
- maven Java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
如果你可以确认你的maven Dependencies中已经导入了如下的jar包,那么你就要检查下Deployment Assembly 选中项目 alt+enter,然后查看maven依赖有没有被添 ...
- BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)
标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.. 思考:推,. 建立gcd(x,y) = p,然后,x / p与y / p互 ...
- JAVA从本机获取IP地址
JAVA从本机获取IP地址 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多博客,网上一个比较普遍的说法是通过InetAddress.getLocalHost().g ...
- OpenVZ安装指南,一种操作系统级别的虚拟化技术
鼎鼎大名的 OpenVZ 谁不知道?在主机行业被使用(滥用)很多年,依然在茁壮发展.作为一种操作系统级别的虚拟化技术,运行 OpenVZ 没有硬性的硬件要求.OpenVZ 能够创建被称为容器(cont ...