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="350" Width="525" Loaded="Window_Loaded_1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox Name="cmbProducts" DisplayMemberPath="ModelName" Text="{Binding Path=ModelName}"
SelectionChanged="cmbProducts_SelectionChanged_1"></ComboBox>
<Grid Grid.Row="1">
<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();
cmbProducts.ItemsSource = products;
this.DataContext = products;
view = (ListCollectionView)CollectionViewSource.GetDefaultView(products);
//view.Filter = new Predicate<object>(FilterProduct);
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();
}
private void cmbProducts_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
view.MoveCurrentTo(cmbProducts.SelectedItem);
}
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;
}
}
}
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;
}
}
}