WPF 精修篇 DataGrid 筛选

DataGrid也可以分组 但是用的地方不多 就没写

筛选还是可以的 比如Datagrid数据量比较大 要做数据筛选

贴码

  <DataGrid x:Name="datagrid"  AutoGenerateColumns="False" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Address}" Header="Address"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Age}" Header="Age"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <CheckBox x:Name="sort" Content="排序" HorizontalAlignment="Left" Margin="466,300,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked"/>
        <TextBox x:Name="TxT" HorizontalAlignment="Left" Height="25" Margin="402,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="115" TextChanged="TextBox_TextChanged"/>
        <TextBlock HorizontalAlignment="Left" Height="20" Margin="336,8,0,0" TextWrapping="Wrap" Text="筛选内容" VerticalAlignment="Top" Width="61"/>


    </Grid>
        public MainWindow()
        {
            InitializeComponent();
            
                Preson = new ObservableCollection<Preson>() { 
                new Preson() { Name = "慧哥1", Address = "大连1", Age = 31 },
                new Preson() { Name = "慧哥2", Address = "大连2", Age = 32 },
                new Preson() { Name = "慧哥3", Address = "大连3", Age = 33 },
                new Preson() { Name = "慧哥4", Address = "大连4", Age = 34 },
                new Preson() { Name = "慧哥5", Address = "大连5", Age = 35 },
                new Preson() { Name = "123", Address = "大连6", Age = 12 },
                new Preson() { Name = "444", Address = "大连7", Age = 14 },
                new Preson() { Name = "222", Address = "大连8", Age = 33 },
                new Preson() { Name = "1312", Address = "大连9", Age = 22 }
            };

                datagrid.ItemsSource = Preson;
          
        }



        public ObservableCollection<Preson> Preson
        {
            get { return (ObservableCollection<Preson>)GetValue(PresonProperty); }
            set { SetValue(PresonProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Preson.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PresonProperty =
            DependencyProperty.Register("Preson", typeof(ObservableCollection<Preson>), typeof(MainWindow), new PropertyMetadata(null));

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            var cvs = CollectionViewSource.GetDefaultView(datagrid.ItemsSource);
            if(cvs!=null&&cvs.CanSort)
            {
                cvs.SortDescriptions.Clear();
                if (sort.IsChecked == true)
                {
                    cvs.SortDescriptions.Add(new System.ComponentModel.SortDescription("Age", System.ComponentModel.ListSortDirection.Descending));
                }
              
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var cvs = CollectionViewSource.GetDefaultView(datagrid.ItemsSource);
            if (cvs != null && cvs.CanFilter)
            {
                cvs.Filter = OnFilterApplied;
            }
        }

        private bool OnFilterApplied(object obj)
        {
            if (obj is Preson) 
            {
                var text = TxT.Text.ToLower();
                return (obj as Preson).Name.ToLower().Contains(text)
                    || (obj as Preson).Address.ToLower().Contains(text);
            }
            return false;
        }

        

 

        
    }

啊~ 这里包含排序 那就包含吧~~

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小慧哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值