WPF 自定义范围分组

<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"));
            PriceRangeProductGrouper grouper = new PriceRangeProductGrouper();
            grouper.GroupInterval = 50;
            view.GroupDescriptions.Add(new PropertyGroupDescription("UnitCost",grouper));



            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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;


namespace ViewExam
{
    public class PriceRangeProductGrouper:IValueConverter
    {
        public int GroupInterval { get; set; }


        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            decimal price = (decimal)value;
            if (price < GroupInterval)
            {
                return string.Format(culture, "Less than {0:C}", GroupInterval);
            }
            else
            {
                int interval = (int)price / GroupInterval;
                int lowerLimit = interval * GroupInterval;
                int upperLimit = (interval + 1) * GroupInterval;
                return string.Format(culture, "{0:C} to {1:C}", lowerLimit, upperLimit);
            }
        }


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}



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

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/80497763