<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. 《iOS Human Interface Guidelines》——Edit Menu

    编辑菜单 用户能够显示一个编辑菜单来在文本视图.网页视图和图像视图运行诸如剪切.粘贴和选择的操作. 你能够调整一些菜单的行为来在你的app中给用户给多的内容控制.比方你能够: 指定哪一个标准菜单命令对 ...

  2. Java编程思想第四版 *第五章 个人练习

    练习3:(1)创建一个带默认构造器(即无參构造器)的类.在构造器中打印一条消息.为这个类创建一个对象.P116 public class Test{ public Test(){ System.out ...

  3. PHP 根据对象属性进行对象数组的排序(usort($your_data, "cmp");)(inside the class: usort($your_data, array($this, "cmp")))

    PHP 根据对象属性进行对象数组的排序(usort($your_data, "cmp");)(inside the class: usort($your_data, array($ ...

  4. 关于 rman duplicate from active database 搭建dataguard--系列一

    关于 rman duplicate from active database.详细操作实际为backup as copy .会拷贝非常多空块.对于那些数据库数据文件超过100G的都不是非常建议用:在非 ...

  5. oracle改动登录认证方式

    通过配置sqlnet.ora文件.我们能够改动oracle登录认证方式. SQLNET.AUTHENTICATION_SERVICES=(NTS);基于操作系统的认证 SQLNET.AUTHENTIC ...

  6. SQL Server2008生成数据库字典

    1.我们在开发过程中可能会遇到这样的一种情况"当我们进行维护其他人的项目时或者项目的二次开发时可能会对原始的数据表进行分析",这里为大家介绍一种方便快捷生成数据库字典的方式. 我们 ...

  7. 【u220】生日礼物

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 一对双胞胎兄妹同一天过生日,这一天,他们的朋友给他俩送来了礼物,每个人送的礼物都是2本书,一本给哥哥, ...

  8. HDOJ 1261 字串数

    JAVA大数.... 字串数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. uiwebview的常用属性1-故事版

  10. 一个完整配置例nginx.conf(生产环境中使用)

    一个完整的nginx配置案例,生产环境 一个完整配置例(生产环境中使用) user nobody nobody; worker_processes 4; worker_rlimit_nofile 51 ...