Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 取消选择RowDetailsTemplate后调整Datagrid高度_C#_Wpf_Wpftoolkit_Wpfdatagrid - Fatal编程技术网

C# 取消选择RowDetailsTemplate后调整Datagrid高度

C# 取消选择RowDetailsTemplate后调整Datagrid高度,c#,wpf,wpftoolkit,wpfdatagrid,C#,Wpf,Wpftoolkit,Wpfdatagrid,我正在使用RowDetailsTemplate显示一行的嵌套数据网格。现在,当我选择一行来显示这个嵌套的datagrid时,datagrid在高度上展开。但取消选择行时,它不会降低其高度 行详细信息折叠后,是否有方法将datagrid调整到其原始高度 有可能以声明的方式进行吗 找到解决此问题的方法;在网格的selection changed事件触发网格项刷新时,这将导致网格重新绘制自身 private void dgVehicles_SelectionChanged(object sen

我正在使用RowDetailsTemplate显示一行的嵌套数据网格。现在,当我选择一行来显示这个嵌套的datagrid时,datagrid在高度上展开。但取消选择行时,它不会降低其高度

  • 行详细信息折叠后,是否有方法将datagrid调整到其原始高度

  • 有可能以声明的方式进行吗


  • 找到解决此问题的方法;在网格的selection changed事件触发网格项刷新时,这将导致网格重新绘制自身

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

    这对我有用。希望有帮助。

    设置
    DataGrid.VerticalAlignment=System.Windows.VerticalAlignment.Top
    使用以下行为将细节放置到堆栈面板和网格中:

    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;
                }
            }
        }
    }
    
    公共类DataGridDetailResizeBehavior:行为
    {
    受保护的覆盖无效附加()
    {
    base.onatached();
    this.AssociatedObject.SizeChanged+=新的SizeChangedEventHandler(元素\u SizeChanged);
    }
    附加时受保护的覆盖无效()
    {
    this.AssociatedObject.SizeChanged-=新的SizeChangedEventHandler(元素\u SizeChanged);
    base.OnDetaching();
    }
    私有void元素\u SizeChanged(对象发送方,SizeChangedEventArgs e)
    {
    //查找DataGridDetailsPresenter
    DataGridDetailsPresenter-rowDetailPresenter=null;
    var元素=this.AssociatedObject;
    while(元素!=null)
    {
    rowDetailPresenter=作为DataGridDetailsPresenter的元素;
    如果(rowDetailPresenter!=null)
    {
    打破
    }
    元素=(FrameworkElement)VisualTreeHelper.GetParent(元素);
    } 
    如果(rowDetailPresenter!=null)
    {
    var row=UIHelper.GetParentOf(this.AssociatedObject);
    if(row!=null&&row.DetailsVisibility==Visibility.Visible)
    {
    //设定高度
    rowDetailPresenter.ContentHeight=this.AssociatedObject.ActualHeight;
    }
    }
    }
    }
    
    XAML如下所示:

    <sdk:DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <StackPanel>
                <Grid>
                    <sdk:DataGrid...
    
                    <i:Interaction.Behaviors>
                        <myinteractivity:DataGridDetailResizeBehavior />
                    </i:Interaction.Behaviors>
                </Grid>
            </StackPanel>
        </DataTemplate>
    </sdk:DataGrid.RowDetailsTemplate>
    
    
    
    注意:如果您需要嵌套的
    DataGrid
    进行独立滚动,那么这将不适用于您。OP的问题中没有提到这个细节

    我意识到这是一条古老的线索,但我在寻找解决问题的方法时偶然发现了它,并认为其他人可能会喜欢看我发现了什么。我没有尝试HolaJan建议的行为方法,因为我在寻找一个更干净的解决方案。也就是说,我确实在MSDN论坛上找到了一篇帖子,在
    DataGrid
    上声明性地使用了
    ScrollViewer.CanContentScroll=“False”

    我找到解决方案的帖子位于:

    答案在标记的答案中,是:
    “我似乎通过设置一个完全无关的环境来解决这个问题

    在我的子网格中,我将ScrollViewer.CanContentScroll设置为
    True
    。一旦我在所有子网格中将其设置为False,它似乎神奇地工作了。现在,当我折叠行详细信息时,它会相应地调整包含行的大小。”