<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="437.165" Width="553.161" Loaded="Window_Loaded_1">
    <Grid>
        <Grid.RowDefinitions>

            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <ListBox Name="lstProducts" DisplayMemberPath="ModelName" Height="200" SelectionChanged="lstProducts_SelectionChanged_1">
            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White" Background="LightGreen" Margin="5,0,0,0" Padding="3"></TextBlock>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListBox.GroupStyle>
        </ListBox>
        <Grid Grid.Row="1" DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}">
            <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();

            lstProducts.ItemsSource = products;

            this.DataContext = products;
            view = (ListCollectionView)CollectionViewSource.GetDefaultView(products);
            //view.Filter = new Predicate<object>(FilterProduct);

            view.SortDescriptions.Add(new System.ComponentModel.SortDescription("CategoryID", System.ComponentModel.ListSortDirection.Ascending));
            view.SortDescriptions.Add(new System.ComponentModel.SortDescription("UnitCost", System.ComponentModel.ListSortDirection.Ascending));

            view.GroupDescriptions.Add(new PropertyGroupDescription("CategoryID"));

            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();
        }

        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;
        }

        private void lstProducts_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            view.MoveCurrentTo(lstProducts.SelectedItem);
        }
    }

}

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. WPF 视图分组排序

    视图分组排序 效果: 实现步骤: 第一步:为分组做一个标题头,就是效果图中的浅蓝色部分: <DataGrid.GroupStyle>标签部分: <DataGrid x:Name=&q ...

  2. WPF DataGrid分组和排序

    之前一直用的Dev的GridControl,控件自带分组排序啥的.今天试了下在wpf自带的Datagrid控件上实现分组和排序. Datagrid上实现这些功能主要用到CollectionViewSo ...

  3. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  4. mongodb group操作 以及管道 aggregate 分组排序分页

    分组获取数据: db.express_info.group({ "key":{"express_code":true}, "initial" ...

  5. 一条Sql语句分组排序并且限制显示的数据条数

    如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...

  6. SQL语句分组排序,多表关联排序

    SQL语句分组排序,多表关联排序总结几种常见的方法: 案例一: 在查询结果中按人数降序排列,若人数相同,则按课程号升序排列? 分析:单个表内的多个字段排序,一般可以直接用逗号分割实现. select ...

  7. oracle 分组排序函数

    项目开发中,我们有时会碰到需要分组排序来解决问题的情况:1.要求取出按field1分组后,并在每组中按照field2排序:2.亦或更加要求取出1中已经分组排序好的前多少行的数据 这里通过一张表的示例和 ...

  8. oracle中分组排序函数用法 - 转

    项目开发中,我们有时会碰到需要分组排序来解决问题的情况,如:1.要求取出按field1分组后,并在每组中按照field2排序:2.亦或更加要求取出1中已经分组排序好的前多少行的数据 这里通过一张表的示 ...

  9. List对象分组排序算法

    场景: List里面的对象是订单的节点,比如我们快递的物流状态,这个是需要有序的,所以需要根据订单号进行分组排序. import java.util.ArrayList; import java.ut ...

随机推荐

  1. Windows Phone 8.1 数据处理

    Windows Phone 8.1 应用的数据存储位置包括: Installation Folder ApplicationData Credential Locker Known Folders S ...

  2. Java多线程系列-线程创建

    1.怎样创建多线程? Java从语言级别实现多线程,因此实现一个多线程程序很easy.有两种方法能够实现多线程,即继承Thread类和实现Runnable接口.由于Java不支持多继承的原因,建议尽可 ...

  3. ssh登录很慢,登录上去后速度正常问题的解决方法

    1. DNS反向解析的问题 OpenSSH在用户登录的时候会验证IP,它根据用户的IP使用反向DNS找到主机名,再使用DNS找到IP地址,最后匹配一下登录的IP是否合法.如果客户机的IP没有域名,或者 ...

  4. 【b504】等价表达式(NOIP2005第4题)

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数 ...

  5. tip of Firefox extention foxyproxy

    tip of Firefox extention foxyproxy

  6. 【codeforces 787B】Not Afraid

    [题目链接]:http://codeforces.com/contest/787/problem/B [题意] -水题..题目太吓人 [题解] 只要你在一组里面找到两个数字,它们的绝对值相同,但是正负 ...

  7. hadoop 3.x 关闭安全模式

    hdfs启动后发现进入了安全模式,最开始使用hdfs dfsadmin -safemode leave来进行关闭发现无法关闭,只好使用hdfs dfsadmin -safemode forceExit ...

  8. 查询系统状态 内存大小 cpu信息 设备负载情况

    1.1 查看内存状态 /proc/meminfo里面存放着内存的信息 查看内存命令(包括虚拟内存swap): free -h (低版本系统可能不支持-h) 或者 free -m (以mb单位显示) a ...

  9. docker-windows随笔资料整理

    背景 业务需求:优化机器学习,IOT边缘计算性能,替换现有的虚拟机部署方案. 技术背景: .netcore python Docker 学习资料 Docker中文社区: http://www.dock ...

  10. 五笔字根--good

    https://gss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/4b90f603738da977b1b5ce57b251f8198 ...