DataGrid列顯示隱藏配置
阿新 • • 發佈:2020-10-23
1.列右鍵事件
private void data1_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { ContextMenu context = new ContextMenu(); MenuItem item = new MenuItem(); item.Header = "請選擇要隱藏顯示的列"; context.Items.Add(item); for (int i = 0; i < data1.Columns.Count;i++) { CheckBox box = new CheckBox(); box.Content = data1.Columns[i].Header; if (data1.Columns[i].Visibility == Visibility.Collapsed) { box.IsChecked = true; } box.Click += new RoutedEventHandler(box_click); context.Items.Add(box); } context.IsOpen = true; ContextMenuService.SetContextMenu(data1,context); }
2.對應列複選框選中事件
1 private void box_click(object sender, RoutedEventArgs e) 2 { 3 CheckBox box = (CheckBox)sender; 4 for (int i = 0; i < data1.Columns.Count;i++) { 5 if (box.Content == data1.Columns[i].Header) { 6 if (data1.Columns[i].Visibility == Visibility.Collapsed)7 data1.Columns[i].Visibility = Visibility.Visible; 8 else 9 data1.Columns[i].Visibility = Visibility.Collapsed; 10 } 11 } 12 }