Reputation: 4127
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.
Is there a way to resize the datagrid to its original height after the row details have been collapsed?
Is it possible to do it declaratively?
Upvotes: 5
Views: 1697
Reputation: 1
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 Grid
s it seemed to magically work. Now when I collapse my row details it resizes the containing row appropriately."
Upvotes: 0
Reputation: 1
Set the DataGrid.VerticalAlignment = System.Windows.VerticalAlignment.Top
Upvotes: -1
Reputation: 916
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.
Upvotes: 2
Reputation: 11
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.
Upvotes: 1