|
| 1 | +# Selection |
| 2 | + |
| 3 | +Two selection modes are supported: |
| 4 | + |
| 5 | +- Row selection allows the user to select whole rows |
| 6 | +- Cell selection allows the user to select individial cells |
| 7 | + |
| 8 | +Both selection types support either single or multiple selection. The default selection type is single row selection. |
| 9 | + |
| 10 | +## Index Paths |
| 11 | + |
| 12 | +Because `TreeDataGrid` supports hierarchical data, using a simple index to identify a row in the data source isn't enough. Instead indexes are represented using the `IndexPath` struct. |
| 13 | + |
| 14 | +An `IndexPath` is essentially an array of indexes, each element of which specifies the index at a succesively deeper level in the hierarchy of the data. |
| 15 | + |
| 16 | +Consider the following data source: |
| 17 | + |
| 18 | +``` |
| 19 | +|- A |
| 20 | +| |- B |
| 21 | +| |- C |
| 22 | +| |- D |
| 23 | +|- E |
| 24 | +``` |
| 25 | + |
| 26 | +- `A` has an index path of `0` as it's the first item at the root of the hierarchy |
| 27 | +- `B` has an index path of `0,0` as it's the first child of the first item |
| 28 | +- `C` has an index path of `0,1` as it's the second child of the first item |
| 29 | +- `D` has an index path of `0,1,0` as it's the first child of `C` |
| 30 | +- `E` has an index path of `1` as it's the second item in the root |
| 31 | + |
| 32 | +`IndexPath` is an immutable struct which is constructed with an array of integers, e.g.: `new ItemPath(0, 1, 0)`. There is also an implicit conversion from `int` for when working with a flat data source. |
| 33 | + |
| 34 | +## Row Selection |
| 35 | + |
| 36 | +Row selection is the default and is exposed via the `RowSelection` property on the `FlatTreeDataGridSource<TModel>` and `HierarchicalTreeDataGridSource<TModel>` classes when enabled. Row selection is stored in an instance of the `TreeDataGridRowSelectionModel<TModel>` class. |
| 37 | + |
| 38 | +By default is single selection. To enable multiple selection set the the `SingleSelect` property to `false`, e.g.: |
| 39 | + |
| 40 | +```csharp |
| 41 | +Source = new FlatTreeDataGridSource<Person>(_people) |
| 42 | +{ |
| 43 | + Columns = |
| 44 | + { |
| 45 | + new TextColumn<Person, string>("First Name", x => x.FirstName), |
| 46 | + new TextColumn<Person, string>("Last Name", x => x.LastName), |
| 47 | + new TextColumn<Person, int>("Age", x => x.Age), |
| 48 | + }, |
| 49 | +}; |
| 50 | + |
| 51 | +Source.RowSelection!.SingleSelect = false; |
| 52 | +``` |
| 53 | + |
| 54 | +The properties on `ITreeDataGridRowSelectionModel<TModel>` can be used to manipulate the selection, e.g.: |
| 55 | + |
| 56 | +```csharp |
| 57 | +Source.RowSelection!.SelectedIndex = 1; |
| 58 | +``` |
| 59 | + |
| 60 | +Or |
| 61 | + |
| 62 | +```csharp |
| 63 | +Source.RowSelection!.SelectedIndex = new IndexPath(0, 1); |
| 64 | +``` |
| 65 | + |
| 66 | +## Cell Selection |
| 67 | + |
| 68 | +To enable cell selection for a `TreeDataGridSource`, assign an instance of `TreeDataGridCellSelectionModel<TModel>` to the source's `Selection` property: |
| 69 | + |
| 70 | +```csharp |
| 71 | +Source = new FlatTreeDataGridSource<Person>(_people) |
| 72 | +{ |
| 73 | + Columns = |
| 74 | + { |
| 75 | + new TextColumn<Person, string>("First Name", x => x.FirstName), |
| 76 | + new TextColumn<Person, string>("Last Name", x => x.LastName), |
| 77 | + new TextColumn<Person, int>("Age", x => x.Age), |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +Source.Selection = new TreeDataGridCellSelectionModel<Person>(Source); |
| 82 | +``` |
| 83 | + |
| 84 | +Or for multiple cell selection: |
| 85 | + |
| 86 | +```csharp |
| 87 | +Source.Selection = new TreeDataGridCellSelectionModel<Person>(Source) { SingleSelect = false }; |
| 88 | +``` |
| 89 | + |
| 90 | +Cell selection is is exposed via the `CellSelection` property on the `FlatTreeDataGridSource<TModel>` and `HierarchicalTreeDataGridSource<TModel>` classes when enabled. |
| 91 | + |
| 92 | +The `CellIndex` struct indentifies an individual cell with by combination of an integer column index and an `IndexPath` row index. |
0 commit comments