WPF datagrid筛选行显影

<!---------xaml------->
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>
    <DataGrid x:Name="lstPeople" Width="200">
        <DataGrid.Columns>
            <DataGridTextColumn Header="className" Width="50" Binding="{Binding className}"/>
            <DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Age" Width="50" Binding="{Binding Age}"/>
            <DataGridTextColumn Header="Sex" Width="80" Binding="{Binding Sex}" />
            <DataGridTextColumn Header="Email" Width="100" Binding="{Binding Email}"/>
        </DataGrid.Columns>
    </DataGrid>
    <StackPanel Orientation="Horizontal" Grid.Row="1">
        <Button x:Name="btnSortbylast" Content="Sort By LastName" Click="btnSortbylast_Click" />
        <Button x:Name="btnSortbydob" Content="Sort By DOB" Click="btnSortbydob_Click" />
        <TextBox Text="1" Width="30" x:Name="filterClass"/>
        <Button x:Name="btnFilter" Content="筛选班级" Click="btnFilter_Click" />
    </StackPanel>
</Grid>

  

// ----------cs------------
public MainWindow()
{
    InitializeComponent();
    InitData();
}

private void InitData()
{
    for (int i = 0; i < 30; i++)
    {
        people.Add(new Person() { Name = "学生" + i, Email = "123333@.abc.com", Sex = "男", Age = "10", className = i.ToString("00").Substring(0, 1) });
    }
            
    lstPeople.ItemsSource = people;
}

private Collection<Person> people = new Collection<Person>();
private bool isfiltered = false;

private void btnSortbylast_Click(object sender, RoutedEventArgs e)
{
    //CollectionView, 表示用于分组、排序、筛选和导航数据集合的视图。
    CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
    cv.SortDescriptions.Clear();
    cv.SortDescriptions.Add(new SortDescription("LastName", ListSortDirection.Ascending));
}

private void btnSortbydob_Click(object sender, RoutedEventArgs e)
{
    CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
    cv.SortDescriptions.Clear();
    cv.SortDescriptions.Add(new SortDescription("Dateofbirth", ListSortDirection.Descending));
}
string className = "";
private void btnFilter_Click(object sender, RoutedEventArgs e)
{
    CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
    if (isfiltered)
    {
        cv.Filter = null;
        isfiltered = false;
    }
    else
    {
        className = filterClass.Text;
        cv.Filter = new Predicate<object>(FilterBySmith);
        isfiltered = true;
    }
}

private bool FilterBySmith(object item)
{
    Person p = item as Person;
    return p.className.Trim() == className;
}

// ------------Person-----------
public class Person
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Sex { get; set; }
    public string Email { get; set; }
    public string className { get; set; }
}

  

WPF datagrid筛选行显影

上一篇:windows环境配置多个tomcat


下一篇:WebApi接口防止表单重复提交最简单粗暴的解决方案