views:

292

answers:

3

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

A: 

One option is to use Grid instead of Datagrid. You can use the SharedSizeGroup attribute on individual column definitions that share a common size.

A: 

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:

public MyWindow()
{
    InitializeComponent();
    PropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(DataGridColumn.WidthProperty, typeof(DataGridColumn));
    pd.AddValueChanged(testColumn, new EventHandler(dgMain_WidthPropertyChanged));
    // do the same for all columns
}

private void dgMain_WidthPropertyChanged(object sender, EventArgs e)
{
    SyncColumnWidths();
}

private void SyncColumnWidths() // put all this in a try..catch too
{
    ScrollContentPresenter scp = dgMain.FindChild(null, typeof(ScrollContentPresenter)) as ScrollContentPresenter;
    ItemsPresenter ip = scp.Content as ItemsPresenter;
    DataGridRowsPresenter dgrp = ip.FindChild(null, typeof(DataGridRowsPresenter)) as DataGridRowsPresenter;
    foreach (DataGridRow dgr in dgrp.Children)
    {
        if (dgr.DetailsVisibility == Visibility.Visible)
        {
            DataGrid dg = dgr.FindChild(null, typeof(DataGrid)) as DataGrid; // child datagrid
            if (dg != null)
            {
                for (int i = 0; i < dgMain.Columns.Count; i++)
                    dg.Columns[i].Width = dgMain.Columns[i].ActualWidth;
            }
        }
    }
}
AdamD
+1  A: 

Using this Avalon (pre WPF) team blog post, I managed to get something that worked. I thought I had implemented it with the DataGrid as well, I searched my code and came up with nothing though. Regardless, if you are determined you'll figure it out and hopefully this will help.

http://blogs.msdn.com/atc_avalon_team/archive/2006/03/01/541206.aspx

CStick
thanks, it looks helpful if datagrids don't end up working for me.
AdamD