Resize Datagrid Height after deselecting a RowDetailsTemplate

淺唱寂寞╮ 提交于 2019-12-10 14:55:31

问题


I am using RowDetailsTemplate to display a nested datagrid for a row. Now the datagrid expands in height when I select a row to show this nested datagrid. But it doesn't reduce its height when the row is deselected.

  1. Is there a way to resize the datagrid to its original height after the row details have been collapsed?

  2. Is it possible to do it declaratively?


回答1:


Place detail into StackPanel and grid with this this Behaviour:

public class DataGridDetailResizeBehavior : Behavior<FrameworkElement>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.SizeChanged += new SizeChangedEventHandler(Element_SizeChanged);
    }

    protected override void OnDetaching()
    {
        this.AssociatedObject.SizeChanged -= new SizeChangedEventHandler(Element_SizeChanged);
        base.OnDetaching();
    }

    private void Element_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        //Find DataGridDetailsPresenter
        DataGridDetailsPresenter rowDetailPresenter = null;
        var element = this.AssociatedObject;
        while (element != null)
        {
            rowDetailPresenter = element as DataGridDetailsPresenter;
            if (rowDetailPresenter != null)
            {
                break;
            }

            element = (FrameworkElement)VisualTreeHelper.GetParent(element);
        } 

        if (rowDetailPresenter != null)
        {
            var row = UIHelper.GetParentOf<DataGridRow>(this.AssociatedObject);
            if (row != null && row.DetailsVisibility == Visibility.Visible)
            {
                //Set height
                rowDetailPresenter.ContentHeight = this.AssociatedObject.ActualHeight;
            }
        }
    }
}

and XAML looks like this:

<sdk:DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <StackPanel>
            <Grid>
                <sdk:DataGrid...

                <i:Interaction.Behaviors>
                    <myinteractivity:DataGridDetailResizeBehavior />
                </i:Interaction.Behaviors>
            </Grid>
        </StackPanel>
    </DataTemplate>
</sdk:DataGrid.RowDetailsTemplate>

This worked for me.




回答2:


Found a work around for this problem; on the selection changed event for the grid trigger a refresh of the grids items, this causes the grid to redraw itself.

    private void dgVehicles_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dg = sender as DataGrid;
        if (dg != null)
        {
            dg.Items.Refresh();
        }
        e.Handled = true;
    }

This worked for me. Hope it helps.




回答3:


Note: If you need your nested DataGrid to have independent scrolling then this won't work for you. That detail wasn't mentioned in the OP's Question.

I realize this is an old thread but I stumbled on it while looking for a way to solve my problem and thought others might like to see what I found. I did not try the behavior approach suggested by HolaJan because I was looking for in my opinion a cleaner solution to my issue. That said I did find a post on an MSDN forum for using ScrollViewer.CanContentScroll="False" declaratively on the DataGrid.

The post where I found my solution is at: http://social.msdn.microsoft.com/Forums/is/wpf/thread/a0e7aea8-e9ad-441f-a775-1178aab75fb0

The answer lies in the marked answer and is:
"I seemed to have resolved the issue by setting a completely unrelated setting.

In my child grids I had ScrollViewer.CanContentScroll set to True. Once I set it to False in all the Child Grids it seemed to magically work. Now when I collapse my row details it resizes the containing row appropriately."




回答4:


Set the DataGrid.VerticalAlignment = System.Windows.VerticalAlignment.Top



来源:https://stackoverflow.com/questions/2364858/resize-datagrid-height-after-deselecting-a-rowdetailstemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!