wpf GridControl selectAll UnSelect
1 <Window x:Class="WpfApplication1.GridControlView" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 5 xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 6 xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 7 Title="GridControlView" > 8 <Grid> 9 <StackPanel> 10 <Button Click="BtnInvert_Click">Invert selection</Button> 11 <Button Click="BtnGetSelected_Click">Get selection</Button> 12 <dxg:GridControl x:Name="grid" CustomUnboundColumnData="grid_CustomUnboundColumnData" Height="500" ItemsSource="{Binding List}"> 13 <dxg:GridControl.Columns> 14 <dxg:GridColumn FieldName="Id" /> 15 <dxg:GridColumn FieldName="Number" /> 16 <dxg:GridColumn FieldName="Selected" UnboundType="Boolean" AllowSorting="False"> 17 <dxg:GridColumn.CellTemplate> 18 <DataTemplate> 19 <dxe:CheckEdit IsChecked="{Binding Data.Selected}" HorizontalAlignment="Center" VerticalAlignment="Center" /> 20 </DataTemplate> 21 </dxg:GridColumn.CellTemplate> 22 </dxg:GridColumn> 23 </dxg:GridControl.Columns> 24 <dxg:GridControl.View> 25 <dxg:TableView x:Name="view" AutoWidth="True" /> 26 </dxg:GridControl.View> 27 </dxg:GridControl> 28 </StackPanel> 29 </Grid> 30 </Window>
1 /// <summary> 2 /// GridControlView.xaml 的交互逻辑 3 /// </summary> 4 public partial class GridControlView : Window 5 { 6 public GridControlView() 7 { 8 InitializeComponent(); 9 //Dxsl.LoginManage.ViewModels.LookupControlViewModel viewModel = new Dxsl.LoginManage.ViewModels.LookupControlViewModel(); 10 11 //this.DataContext = viewModel; 12 DataContext = data; 13 } 14 15 TestData data = new TestData(); 16 SelectionHelper<Guid> selectionHelper = new SelectionHelper<Guid>(); 17 18 /// <summary> 19 /// Grid触发事件 20 /// </summary> 21 /// <param name="sender"></param> 22 /// <param name="e"></param> 23 private void grid_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e) 24 { 25 if (e.Column.FieldName != "Selected") return; 26 Guid key = (Guid)e.GetListSourceFieldValue("Id"); 27 if (e.IsGetData) 28 e.Value = selectionHelper.GetIsSelected(key); 29 if (e.IsSetData) 30 selectionHelper.SetIsSelected(key, (bool)e.Value); 31 } 32 /// <summary> 33 /// 全选或者反选 34 /// </summary> 35 /// <param name="sender"></param> 36 /// <param name="e"></param> 37 private void BtnInvert_Click(object sender, RoutedEventArgs e) 38 { 39 for (int i = 0; i < data.List.Count; i++) 40 { 41 int rowHandle = this.grid.GetRowHandleByListIndex(i); 42 bool newIsSelected = !selectionHelper.GetIsSelected(data.List[i].Id); 43 grid.SetCellValue(rowHandle, "Selected", newIsSelected); 44 } 45 } 46 /// <summary> 47 /// 获取选中项 48 /// </summary> 49 /// <param name="sender"></param> 50 /// <param name="e"></param> 51 private void BtnGetSelected_Click(object sender, RoutedEventArgs e) 52 { 53 string caption = string.Format("Selected rows (Total: {0})", selectionHelper.GetSelectionCount()); 54 MessageBox.Show(selectionHelper.GetSelectedKeysAsString(), caption); 55 } 56 }
1 /// <summary> 2 /// 选择帮助类 3 /// </summary> 4 /// <typeparam name="KeyType">类型</typeparam> 5 public class SelectionHelper<KeyType> 6 { 7 /// <summary> 8 /// 选中集合字典 9 /// </summary> 10 Dictionary<KeyType, bool> selectedValues = new Dictionary<KeyType, bool>(); 11 /// <summary> 12 /// 获取key是否被选中 13 /// </summary> 14 /// <param name="key">key 值</param> 15 /// <returns></returns> 16 public bool GetIsSelected(KeyType key) 17 { 18 //声明是否选择变量,默认为false 19 bool isSelected; 20 if (selectedValues.TryGetValue(key, out isSelected)) 21 return isSelected; 22 return false; 23 } 24 /// <summary> 25 /// 设置选中项的值 26 /// </summary> 27 /// <param name="key">key 值</param> 28 /// <param name="value">true or false</param> 29 public void SetIsSelected(KeyType key, bool value) 30 { 31 if (value) 32 selectedValues[key] = value; 33 else 34 selectedValues.Remove(key); 35 } 36 /// <summary> 37 /// 获取集合字典中的选中项 38 /// </summary> 39 /// <returns></returns> 40 public List<KeyType> GetSelectedKeys() 41 { 42 List<KeyType> list = new List<KeyType>(); 43 //遍历集合 44 foreach (KeyType key in selectedValues.Keys) 45 list.Add(key); 46 return list; 47 } 48 /// <summary> 49 /// 获取集合中项 加入StringBuilder 仅测试用 50 /// </summary> 51 /// <returns></returns> 52 public string GetSelectedKeysAsString() 53 { 54 List<KeyType> list = GetSelectedKeys(); 55 StringBuilder str = new StringBuilder(); 56 for (int i = 0; i < list.Count; i++) 57 str.AppendLine(list[i].ToString()); 58 return str.ToString(); 59 } 60 /// <summary> 61 /// 获取选择数量 62 /// </summary> 63 /// <returns></returns> 64 public int GetSelectionCount() 65 { 66 return selectedValues.Count; 67 } 68 }
1 public class TestData 2 { 3 public List<TestDataItem> List { get; private set; } 4 public TestData() 5 { 6 List<TestDataItem> list = new List<TestDataItem>(); 7 for (int i = 0; i < 20; i++) 8 { 9 list.Add(new TestDataItem() { Id = Guid.NewGuid(), Number = i }); 10 } 11 List = list; 12 } 13 }
1 public class TestDataItem 2 { 3 public Guid Id { get; set; } 4 public int Number { get; set; } 5 }