WPF DataGrids in itemsControl with shared Column sizes

153 Views Asked by At

Ok, I need to refactor other one's code and I cannot change it too much. So the problem is the following, there is an ItemsControl with StackPanel as its ItemsPanel. Inside we have DataTemplates, each of them has a DataGrid (it needs to stay that way, so redoing it as DataGrid with grouping is out of the question). Now, the DataGrid has 2 columns. First of them is kinda Auto, second is *. The first ones should share their width.

The DG columns look something like this:

 <DataGrid.Columns>
     <DataGridTemplateColumn x:Name="ItemPropertyNameColumn" 
                      CellTemplate="{StaticResource PropertyGridNameColumnTemplate}"
                      Width="Auto"
                      MaxWidth="145"/>
     <DataGridTemplateColumn x:Name="ItemPropertyValueColumn" 
                      CellTemplate="{StaticResource PropertyGridValueColumnTemplate}"
                      Width="1*"/>
  </DataGrid.Columns>

Inside of a cell is a TextBlock (don't ask...), must stay that way.

1

There are 1 best solutions below

0
On

Thanks everyone, I solved it a bit stranger way, since binding to Grids with SharedSizeScope didn't work in many configurations.

I solved it using a simple DataTemplateColumn as a named resource outside the template and I just bound my first column to the Width of it with Mode - TwoWay.

It's a bit strange, but it worked.