<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 过滤集合的更多相关文章

  1. 使用filter方法过滤集合元素

    文章转自https://my.oschina.net/nenusoul/blog/658238 Problem 你想要筛选出集合中的一些元素形成一个新的集合,这些元素都是满足你的筛选条件的. Solu ...

  2. laravel的filter()方法的使用 (方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容)

    filter 方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容: $collection = collect([1, 2, 3, 4]); $filtered = $collec ...

  3. WPF 支持集合绑定的控件

    WPF 支持集合绑定的控件 ListBox ComboBox ListView DataGrid

  4. 使用Java Stream,提取集合中的某一列/按条件过滤集合/求和/最大值/最小值/平均值

    不得不说,使用Java Stream操作集合实在是太好用了,不过最近在观察生产环境错误日志时,发现偶尔会出现以下2个异常: java.lang.NullPointerException java.ut ...

  5. 使用Guava提供的filter过滤集合

    正常情况下,我们声明一个List需要如下代码 List<String> list = new ArrayList<>(); list.add("AAA"); ...

  6. java8 按条件过滤集合

    //黄色部分为过滤条件list.stream().filter(user-> user.getId() > 5 && "1组".equals(user. ...

  7. asp.net通过distinct过滤集合(list)中重复项的办法

    /// <summary> /// 权限Distinct比较器 /// </summary> public class PermissionIdComparer : IEqua ...

  8. WPF 绑定集合 根据集合个数改变样式 INotifyCollectionChanged

    问题:当前ListBox Items 绑定 集合数据源ListA时候:ListA集合数据源中存在另外一个集合ListB,当更改或往ListB集合中添加数据的时候,通知改变? 实体类继承 INotify ...

  9. java8的stream系列教程之filter过滤集合的一些属性

    贴代码 List<Student> lists = new ArrayList<>(); Student student = new Student(); student.se ...

随机推荐

  1. php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多)

    php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多) 一.总结 这个题目用另外一个栈做单调栈的话时间复杂度会低很多 二.php实现包含min函数的栈 题目描述 定义栈的数 ...

  2. yield return

    一次被yield return坑的历程.   事情的经过是这样的: 我用C#写了一个很简单的一个通过迭代生成序列的函数. public static IEnumerable<T> Iter ...

  3. 驱动程序调试方法之printk——自制proc文件(一)

    首先我们需要弄清楚proc机制,来看看fs/proc/proc_misc.c这个文件,从入口函数开始看: proc_misc_init(void)         #ifdef CONFIG_PRIN ...

  4. Struts2完全解耦和

    jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...

  5. [JS Compose] 5. Create types with Semigroups

    An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...

  6. html5-8 如何控制html5中的视频标签和音频标签

    html5-8 如何控制html5中的视频标签和音频标签 一.总结 一句话总结:找到视频或者音频的element对象,然后查手册看对应的方法或者属性就可以,里面有控制的. 1.如何控制html5中的视 ...

  7. ArcEngine创建IElement简单例子

    转自IT-GIS终结者原文ArcEngine创建IElement简单例子 代码下载地址:http://files.cnblogs.com/ogis/MapControlApplication2.rar ...

  8. android webview中的音乐的暂停与播放

    前段时间有这样一个需求,webview显示一个带音乐的网页,在播放音乐的时候进入第三方软件暂停播放,返回时继续播放.后来参考了两篇文章解决了这个问题. AudioManager audioManage ...

  9. spark 基于key排序的wordcount

    java /** * 根据单词次数排序的wordcount * @author Tele * */ public class SortWordCount { private static SparkC ...

  10. 为什么java的web开发中URLEncoder.encode方法要为什么要调用两次

    一: 我们先看2个编码的情况 String name=java.net.URLEncoder.encode("测试", "UTF-8"); System.out ...