开发者

How to synchronize column widths between two nested datagrids?

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


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


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


I know this is an old question (from 2010), but here is how I solved the same problem. Essentially, I keep a reference to all the nested ListViews as they are created, in the code behind, and then update their widths when my parent "columns" (in my case, faux columns created using a grid, but the same principle applies) have a change in width.

Note - in XAML, I specified ListView_Loaded as the handler for the Loaded event of the child (nested) listview.

Ignore the fact that my parent-column-changing event is GridSplitter_DragDelta, it's because I used GridSplitters to "fake" a top-level header row; in your case, you would get the width of the column being moved in your own event handler, then assign it using the linq statement "childViewColumns.ForEach((gvcc) => gvcc[index].Width", etc.

public partial class SomeThing : UserControl

private List<GridViewColumnCollection> childViewColumns = new List<GridViewColumnCollection>();

        private void ListView_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            GridView gv = ((ListView)sender).View as GridView;
            childViewColumns.Add(gv.Columns);
        }

        private void GridSplitter_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            GridSplitter gs = sender as GridSplitter;
            Grid g = gs.Parent as Grid;
            int index = Grid.GetColumn(gs);
            double w = g.ColumnDefinitions[index].Width.Value;
            childViewColumns.ForEach((gvcc) => gvcc[index].Width = w);

        }

}


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;
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜