Total widths of a DataGridColumn across multiple DataGrids

I am doing several DataGrids in an application with the same set of columns and bindings. I hope all DataGrids respond appropriately to a change in one DataGrid. If I resize one column, the corresponding columns in other DGs should have the same width. This is somewhat similar to this SO question ( column width of WPF partitions between separate grids ), with the exception of DataGrids, not Grids. I was hoping DataGrids would have a property similar to IsSharedSize, as in the Grid, but this does not seem to be the case.

Is there a property that I could access, or some alternative approach, to do what I'm trying to accomplish? Before anyone suggests this, I cannot combine all of them into one DataGrid, what I'm trying to do, I cannot put all the information in one DataGrid due to the nature of the application itself.

+4
source share
2 answers

While general availability in DataGrid's not available, this is what I came across as the best way to deal with such scenarios.

Create bindings between the columns of the DataGrid source and the target column width of the DataGrid . In my case, I have two DataGrid's goals (dgTarget1 and dgTarget2), so here is the code:

 for (int index = 0; index < dgSource.Columns.Count; index++) { Binding bindingWidth = new Binding(); bindingWidth.Mode = BindingMode.TwoWay; bindingWidth.Source = dgSource.Columns[index]; bindingWidth.Path = new PropertyPath(DataGridColumn.WidthProperty); BindingOperations.SetBinding(dgTarget1.Columns[index], DataGridColumn.WidthProperty, bindingWidth); BindingOperations.SetBinding(dgTarget2.Columns[index], DataGridColumn.WidthProperty, bindingWidth); } 
+3
source

A bit late for a party on this, but I came across a similar scenario where I needed a Grid to sit below the DataGrid and share the same columns. You can implement something similar to diggy's answer using only XAML:

 <DataGrid x:Name="dgOne"> <DataGrid.Columns> <DataGridTextColumn Header="One" /> <DataGridTextColumn Header="Two" /> <DataGridTextColumn Header="Three" /> <DataGridTextColumn Header="Four" /> </DataGrid.Columns> </DataGrid> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" /> <ColumnDefinition Width="{Binding Columns[1].ActualWidth, ElementName=dgOne}" /> <ColumnDefinition Width="{Binding Columns[2].ActualWidth, ElementName=dgOne}" /> <ColumnDefinition Width="{Binding Columns[3].ActualWidth, ElementName=dgOne}" /> </Grid.ColumnDefinitions> ... </Grid> 

There is no reason you could not do the same with two DataGrids.

 <DataGrid x:Name="dgOne"> <DataGrid.Columns> <DataGridTextColumn Header="One" /> <DataGridTextColumn Header="Two" /> <DataGridTextColumn Header="Three" /> <DataGridTextColumn Header="Four" /> </DataGrid.Columns> </DataGrid> <DataGrid> <DataGrid.Columns> <DataGridTextColumn Header="One" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" /> <DataGridTextColumn Header="Two" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" /> <DataGridTextColumn Header="Three" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" /> <DataGridTextColumn Header="Four" Width="{Binding Columns[0].ActualWidth, ElementName=dgOne}" /> </DataGrid.Columns> </DataGrid> 
+4
source

All Articles