|
4 | 4 | [](https://docs.devexpress.com/GeneralInformation/403183)
|
5 | 5 | [](#does-this-example-address-your-development-requirementsobjectives)
|
6 | 6 | <!-- default badges end -->
|
7 |
| -<!-- default file list --> |
8 |
| -*Files to look at*: |
9 | 7 |
|
| 8 | +# WPF Data Grid – Resize Row Height with a Splitter |
| 9 | + |
| 10 | +This example configures the [`GridControl`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl) to resize row height with a splitter. The grid uses a custom control that stores row height in the [`RowData.RowState`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.RowData.RowState) property, so the height persists after refresh. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +Use this technique when you need to: |
| 15 | + |
| 16 | +* Allow users to resize rows manually. |
| 17 | +* Persist row height after a user updates data or scrolls a view. |
| 18 | +* Integrate a splitter into the row template. |
| 19 | + |
| 20 | +## Implementation Details |
| 21 | + |
| 22 | +1. Create a custom control that implements the `IResizeHelperOwner` interface. |
| 23 | +2. Add an attached `RowHeight` property to store the current height in the `RowState`. |
| 24 | +3. Define a `DataRowTemplate` for the view that includes the following: |
| 25 | + - A `ContentControl` bound to the [`DefaultDataRowTemplate`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.TableView.DefaultDataRowTemplate) to display the row content. |
| 26 | + - A custom `ResizableDataRow` control with a `RowSplitter` in its template. |
| 27 | +4. Bind the `ContentControl.Height` property to the `RowHeight` property to apply size changes. |
| 28 | + |
| 29 | +### Row Template |
| 30 | + |
| 31 | +```xaml |
| 32 | +<DataTemplate x:Key="PersistentRowStateDataRowTemplate"> |
| 33 | + <StackPanel Orientation="Vertical"> |
| 34 | + <dx:MeasurePixelSnapper> |
| 35 | + <Grid> |
| 36 | + <Grid.RowDefinitions> |
| 37 | + <RowDefinition Height="*"/> |
| 38 | + <RowDefinition Height="Auto"/> |
| 39 | + </Grid.RowDefinitions> |
| 40 | + <ContentControl Content="{Binding}" |
| 41 | + ContentTemplate="{Binding Path=View.DefaultDataRowTemplate}" |
| 42 | + Height="{Binding Path=RowState.(local:ResizableDataRow.RowHeight)}"/> |
| 43 | + </Grid> |
| 44 | + </dx:MeasurePixelSnapper> |
| 45 | + <local:ResizableDataRow> |
| 46 | + <local:ResizableDataRow.Template> |
| 47 | + <ControlTemplate> |
| 48 | + <dxg:RowSplitter Name="PART_Resizer" |
| 49 | + Grid.Row="1" |
| 50 | + Cursor="SizeNS" |
| 51 | + Height="1" /> |
| 52 | + </ControlTemplate> |
| 53 | + </local:ResizableDataRow.Template> |
| 54 | + </local:ResizableDataRow> |
| 55 | + </StackPanel> |
| 56 | +</DataTemplate> |
| 57 | +``` |
| 58 | + |
| 59 | +### Custom Control |
| 60 | + |
| 61 | +The `ResizableDataRow` control implements the `IResizeHelperOwner` interface to work with `ResizeHelper`. The control stores the current row height in the `RowHeight` property and updates this value when the splitter moves. |
| 62 | + |
| 63 | +```csharp |
| 64 | +public class ResizableDataRow : Control, IResizeHelperOwner { |
| 65 | + public static readonly DependencyProperty RowHeightProperty = |
| 66 | + DependencyProperty.RegisterAttached("RowHeight", typeof(double), typeof(ResizableDataRow), new PropertyMetadata(20d)); |
| 67 | + |
| 68 | + double IResizeHelperOwner.ActualSize { get => RowHeight; set => RowHeight = value; } |
| 69 | + void IResizeHelperOwner.ChangeSize(double delta) { |
| 70 | + RowHeight = Math.Min(300, Math.Max(20, RowHeight + delta)); |
| 71 | + } |
| 72 | + // ... |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## Files to Review |
| 77 | + |
| 78 | +* [MainWindow.xaml](./CS/PersistentRowState/MainWindow.xaml) (VB: [MainWindow.xaml](./VB/PersistentRowState/MainWindow.xaml)) |
| 79 | +* [MainWindow.xaml.cs](./CS/PersistentRowState/MainWindow.xaml.cs) (VB: [MainWindow.xaml.vb](./VB/PersistentRowState/MainWindow.xaml.vb)) |
10 | 80 | * [Classes.cs](./CS/PersistentRowState/Classes.cs) (VB: [Classes.vb](./VB/PersistentRowState/Classes.vb))
|
11 | 81 | * [Data.cs](./CS/PersistentRowState/Data.cs) (VB: [Data.vb](./VB/PersistentRowState/Data.vb))
|
12 |
| -* **[MainWindow.xaml](./CS/PersistentRowState/MainWindow.xaml) (VB: [MainWindow.xaml](./VB/PersistentRowState/MainWindow.xaml))** |
13 |
| -* [MainWindow.xaml.cs](./CS/PersistentRowState/MainWindow.xaml.cs) (VB: [MainWindow.xaml.vb](./VB/PersistentRowState/MainWindow.xaml.vb)) |
14 |
| -<!-- default file list end --> |
15 |
| -# WPF Grid - Resize rows using a splitter |
16 | 82 |
|
| 83 | +## Documentation |
17 | 84 |
|
18 |
| -<p>To make the row height resizable, perform these steps:</p> |
19 |
| -<p>1. Create a control class that describes the <strong>IResizeHelperOwner</strong> interface.</p> |
20 |
| -<p>2. Create an attached RowHeight property, add it to this class and assign the property value to the RowData’s RowState property.</p> |
21 |
| -<p>3. In GridControl’s view, create DataTemplate for the view’s DataRowTemplate property that contains ContentControl and the control that was created earlier. Add RowSplitter into the control’s template.</p> |
22 |
| -<p>4. Bind ContentControl’s ContentTemplate property to the view’s DefaultDataRowTemplate to show data. Also, bind ContentControl’s height to the control’s RowHeight property to resize the row.</p> |
| 85 | +* [GridControl](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl) |
| 86 | +* [RowData](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.RowData) |
| 87 | +* [RowState](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.RowData.RowState) |
| 88 | +* [DataRowTemplate](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.TableView.DataRowTemplate) |
| 89 | +* [DefaultDataRowTemplate](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.TableView.DefaultDataRowTemplate) |
23 | 90 |
|
24 |
| -<br/> |
| 91 | +## More Examples |
25 | 92 |
|
| 93 | +* [Implement CRUD Operations in the WPF Data Grid](https://github.com/DevExpress-Examples/wpf-data-grid-implement-crud-operations) |
| 94 | +* [WPF Data Grid – Handle Drag and Drop Operations](https://github.com/DevExpress-Examples/wpf-grid-handle-drag-and-drop) |
| 95 | +* [WPF Data Grid – Specify Custom Content for Column Chooser Headers](https://github.com/DevExpress-Examples/wpf-data-grid-custom-content-for-column-chooser-headers) |
| 96 | +* [WPF Data Grid – Bind to Dynamic Data](https://github.com/DevExpress-Examples/wpf-bind-gridcontrol-to-dynamic-data) |
26 | 97 |
|
27 | 98 | <!-- feedback -->
|
28 | 99 | ## Does this example address your development requirements/objectives?
|
|
0 commit comments