I have a wpf toolkit datagrid with a few columns, then inside the row details I have another datagrid. Basically I am emulating a treelistview control (example: http://www.codeproject.com/KB/list/treelistview.aspx) but using datagrids.
I would like to sync the column widths of the datagrid in the row details with the column widths of the main parent datagrid.
I tried defining a child datagrid column like this:
<toolkit:DataGridTextColumn Binding="{Binding Path=Name}" Width="{Binding ElementName=mainDataGrid, Path=Columns[0].ActualWidth}" />
This did not work (even with some variations like Mode=OneWay).
EDIT:
Ok I gave up on getting the binding to work. Trying with code…
Now, since the DataGrid doesn’t fire an event for column width changed I made my own:
PropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(DataGridColumn.WidthProperty, typeof(DataGridColumn));
pd.AddValueChanged(testColumn, new EventHandler(mainDataGrid_WidthPropertyChanged));
private void mainDataGrid_WidthPropertyChanged(object sender, EventArgs e)
{
// this works fine, but how do I find my templated child datagrid?
}
The event seems to fire correctly, but I can’t seem to find my templated datagrid. Each row has a row details section, but some or all can be hidden. But still even when my row details is visible I cannot find a reference to it (always null).
DataGrid dg = mainDataGrid.FindChild(null, typeof(DataGrid)) as DataGridRow; // dg always null
This is using a ‘FindChild’ method that I know works.
Anyone know what the problem could be? Or any other suggestions? Thanks
OK, I have gotten this working but I am not completely satisfied with it. The problem was with chidren not being detected correctly. Basically I drilled down the visual tree manually at positions that weren’t connected (using Mole to help me). This may not work for others if the visual tree is different, but it is a general idea. Here is the rough version of what I did, but I am still looking over it to see what exactly was happening: