标签:ble reg ado col comment def ring key vat
原文:WPF 精修篇 DataGrid 筛选
DataGrid也可以分组 但是用的地方不多 就没写
筛选还是可以的 比如Datagrid数据量比较大 要做数据筛选
贴码
-
"datagrid" AutoGenerateColumns="False" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
-
-
"{Binding Name}" Header="Name">
-
"{Binding Address}" Header="Address">
-
"{Binding Age}" Header="Age">
-
-
-
"sort" Content="排序" HorizontalAlignment="Left" Margin="466,300,0,0" VerticalAlignment="Top" Checked="CheckBox_Checked"/>
-
"TxT" HorizontalAlignment="Left" Height="25" Margin="402,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="115" TextChanged="TextBox_TextChanged"/>
-
"Left" Height="20" Margin="336,8,0,0" TextWrapping="Wrap" Text="筛选内容" VerticalAlignment="Top" Width="61"/>
-
-
-
-
-
-
-
-
Preson =
new ObservableCollection
() {
-
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
-
-
get {
return (ObservableCollection
)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
), 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)
-
-
-
-
var text = TxT.Text.ToLower();
-
return (obj as Preson).Name.ToLower().Contains(text)
-
|| (obj as Preson).Address.ToLower().Contains(text);
-
-
-
-
-
-
-
-
-
-
啊~ 这里包含排序 那就包含吧~~
WPF 精修篇 DataGrid 筛选
标签:ble reg ado col comment def ring key vat
原文地址:https://www.cnblogs.com/lonelyxmas/p/12075454.html