码迷,mamicode.com
首页 > 编程语言 > 详细

D21_02_view过滤排序filter orderby

时间:2014-11-01 13:15:42      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   os   ar   for   

 

bubuko.com,布布扣

 

 

<Window x:Class="demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
        xmlns:db="clr-namespace:StoreDatabase;assembly=StoreDatabase"
        xmlns:local="clr-namespace:demo"
        Title="FilterCollection" Height="390" Width="628"
    >

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*"></ColumnDefinition>
            <ColumnDefinition Width="5*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>

            <Button Margin="7,7,7,0" Padding="2" Click="cmdGetProducts_Click">Get Products</Button>
            <ListBox Grid.Row="1" Margin="7,3,7,10" Name="lstProducts" DisplayMemberPath="ModelName"></ListBox>


            <Border Grid.Row="2" Margin="7" Padding="7" BorderBrush="SteelBlue" BorderThickness="1" >
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Label>Price > Than</Label>
                    <TextBox Grid.Column="1" Name="txtMinPrice" TextChanged="txtMinPrice_TextChanged">0</TextBox>

                    <Button Grid.Row="1" Margin="2" Padding="2" Click="cmdFilter_Click">Filter</Button>
                    <Button Grid.Row="1" Grid.Column="1" Margin="2" Padding="2" Click="cmdRemoveFilter_Click">Remove Filter</Button>

                </Grid>
            </Border>
        </Grid>

        <GridSplitter Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Stretch"
                   Width="5"></GridSplitter>

        <Border Grid.Column="1" Padding="7" Margin="7" Background="LightSteelBlue">
            <Grid DataContext="{Binding ElementName=lstProducts, Path=SelectedItem}" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                </Grid.RowDefinitions>

                <TextBlock Margin="7">Model Number:</TextBlock>
                <TextBox Margin="5" Grid.Column="1" Text="{Binding Path=ModelNumber}"></TextBox>
                <TextBlock Margin="7" Grid.Row="1">Model Name:</TextBlock>
                <TextBox Margin="5" Grid.Row="1" Grid.Column="1" Text="{Binding Path=ModelName}"></TextBox>
                <TextBlock Margin="7" Grid.Row="2">Unit Cost:</TextBlock>
                <TextBox Margin="5" Grid.Row="2" Grid.Column="1" Text="{Binding Path=UnitCost,StringFormat={}{0:C}}"></TextBox>
                <TextBlock Margin="7,7,7,0" Grid.Row="3">Description:</TextBlock>
                <TextBox Margin="7" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" 
                 TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" Text="{Binding Path=Description}"></TextBox>


            </Grid>
        </Border>

    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using StoreDatabase;
using System.ComponentModel;

namespace demo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ICollection<Product> products;

        public MainWindow()
        {
            InitializeComponent();    
       
        }

        private void cmdGetProducts_Click(object sender, RoutedEventArgs e)
        {
            products = App.StoreDb.GetProducts();
            lstProducts.ItemsSource = products;

            ICollectionView view = CollectionViewSource.GetDefaultView(lstProducts.ItemsSource);
            view.SortDescriptions.Add(new SortDescription("ModelName", ListSortDirection.Ascending));
            
        }

        private void cmdFilter_Click(object sender, RoutedEventArgs e)
        {
            decimal minimumPrice;
            if (Decimal.TryParse(txtMinPrice.Text, out minimumPrice))
            {
                ListCollectionView view = (ListCollectionView)CollectionViewSource.GetDefaultView(lstProducts.ItemsSource);

                if (view != null)
                {
                    filterer = new ProductByPriceFilterer(minimumPrice);
                    //Predicate是一个委托,会为FilterItem传递lstProducts.ItemsSource对应的数据集的类型(produncts)
                    view.Filter = new Predicate<object>(filterer.FilterItem);
                }
            }
        }

        private void cmdRemoveFilter_Click(object sender, RoutedEventArgs e)
        {
            ListCollectionView view = CollectionViewSource.GetDefaultView(lstProducts.ItemsSource) as ListCollectionView;
            if (view != null)
            {
                view.Filter = null;
            }
        }

        private ProductByPriceFilterer filterer;

        private void txtMinPrice_TextChanged(object sender, TextChangedEventArgs e)
        {
            ListCollectionView view = CollectionViewSource.GetDefaultView(lstProducts.ItemsSource) as ListCollectionView;
            if (view != null)
            {
                decimal minimumPrice;
                if (Decimal.TryParse(txtMinPrice.Text, out minimumPrice) && (filterer != null))
                {
                    filterer.MinimumPrice = minimumPrice;
                    view.Refresh();
                }
            }
        }
    }

    public class ProductByPriceFilterer
    {
        public decimal MinimumPrice
        {
            get;
            set;
        }

        public ProductByPriceFilterer(decimal minimumPrice)
        {
            MinimumPrice = minimumPrice;
        }

        public bool FilterItem(Object item)
        {
            Product product = (Product)item;
            if (product != null)
            {
                if (product.UnitCost > MinimumPrice)
                {
                    return true;
                }
            }
            return false;
        }
    }
}

StoreDB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.ObjectModel;

namespace StoreDatabase
{
    public class StoreDB
    {
        private string connectionString = StoreDatabase.Properties.Settings.Default.Store;        

        public ICollection<Product> GetProducts()
        {
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("GetProducts", con);
            cmd.CommandType = CommandType.StoredProcedure;

            ObservableCollection<Product> products = new ObservableCollection<Product>();
            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    // Create a Product object that wraps the 
                    // current record.
                    Product product = new Product((string)reader["ModelNumber"],
                        (string)reader["ModelName"], (decimal)reader["UnitCost"],
                        (string)reader["Description"], (int)reader["ProductID"],
                        (string)reader["CategoryName"], (string)reader["ProductImage"]);
                    // Add to collection
                    products.Add(product);
                }
            }
            finally
            {
                con.Close();
            }

            return products;
        }
    }
}

D21_02_view过滤排序filter orderby

标签:des   style   blog   http   io   color   os   ar   for   

原文地址:http://www.cnblogs.com/xiepengtest/p/4066991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!