<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. arcengine 开发经典帖 【强烈推荐仔细研读】

    转自原文 arcengine 开发经典帖 使用ArcGIS Engine 开发自定义GIS应用: 第一部分:使用ArcGIS Engine 发布自定义GIS应用软件-全面了解ArcGIS Engine ...

  2. tspitr(tablespace point in time recovery)实验

    ===========环境模拟================= -----------模拟数据---------------- SYS@ORCL>create tablespace test ...

  3. 《TCP/IP具体解释卷2:实现》笔记--域和协议

    Net/3组把协议关联到一个域,而且用一个协议族常量来标识每一个域.Net/3还通过全部的编址方法将协议分组. 在一个域中 的每一个协议使用同类地址.而且每种地址仅仅被一个域使用.作为结果,一个域能通 ...

  4. 【t077】宝物筛选

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物--这下小FF可发财了.但是这里的宝物实在是太多 ...

  5. [Angular] @ViewChild and template #refs to get Element Ref

    We can use @ViewChild with component: @ViewChild(AuthMessageComponent) message: AuthMessageComponent ...

  6. 【u203】文件夹计数

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 可以用路径来描述文件夹,路径为一个包含若干部分的字符串,之间用'/'分隔.每部分均为一个文件夹的名称, ...

  7. oracle 全部查询和表空间,以及其关系

    select * from dba_users;   查看数据库里面全部用户,前提是你是有dba权限的帐号.如sys,system select * from all_users;     查看你能管 ...

  8. spark cogroup算子

    java /** *cogroup与join算子不同的是如果rdd中的一个key,对应多个value,则返回<Iterable<key>,Iterable<value>& ...

  9. 英文构词法 —— circum- 前缀

    1. - circum-:表示环绕,周围,圆周: circle:圆:循环: circumference:圆周,周长,胸围: circumstance:环境: circumnavigation:环球航行 ...

  10. WebHost failed to process a request.Memory gates checking failed because the free memory (140656640 bytes) is less than 5% of total memory

    WebHost failed to process a request. Sender Information: System.ServiceModel.ServiceHostingEnvironme ...