我正在使用RowDetailsTemplate來顯示行的嵌套數據網格。現在,當我選擇一行以顯示此嵌套數據網格時,datagrid將在高度展開。但是當行被取消選擇時,它不會降低其高度。取消選擇RowDetailsTemplate後調整Datagrid高度
是否有一種方法可以在行細節摺疊後調整datagrid的原始高度?
是否可以做到這一點聲明?
我正在使用RowDetailsTemplate來顯示行的嵌套數據網格。現在,當我選擇一行以顯示此嵌套數據網格時,datagrid將在高度展開。但是當行被取消選擇時,它不會降低其高度。取消選擇RowDetailsTemplate後調整Datagrid高度
是否有一種方法可以在行細節摺疊後調整datagrid的原始高度?
是否可以做到這一點聲明?
找到了解決此問題的方法;在選擇已更改事件的網格觸發器刷新網格項目時,這會導致網格自身重繪。
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
這並沒有做任何事情來解決問題。 – Harv 2012-05-30 19:59:14
將詳細到的StackPanel和電網本該行爲:
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;
}
}
}
}
和XAML看起來像這樣:
<sdk:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<sdk:DataGrid...
<i:Interaction.Behaviors>
<myinteractivity:DataGridDetailResizeBehavior />
</i:Interaction.Behaviors>
</Grid>
</StackPanel>
</DataTemplate>
</sdk:DataGrid.RowDetailsTemplate>
這爲我工作。
什麼是行爲類? – Artiom 2012-08-14 17:06:03
注意:如果您需要嵌套DataGrid
獨立滾動,那麼這不適用於您。 OP的問題中沒有提到這個細節。
我意識到這是一條古老的線索,但是我一直在尋找解決問題的方法,並且認爲其他人可能會喜歡看到我發現的東西。我沒有嘗試HolaJan提出的行爲方法,因爲我一直在尋找一個更清晰的解決方案來解決我的問題。也就是說,我確實在一個MSDN論壇上發現了一篇文章,在DataGrid
上聲明使用ScrollViewer.CanContentScroll="False"
。
,我發現我的解決方案是在一篇:http://social.msdn.microsoft.com/Forums/is/wpf/thread/a0e7aea8-e9ad-441f-a775-1178aab75fb0
答案就在明顯的答案是:
「我似乎已經解決了通過設置一個完全無關的設置問題
在我的子網格中,我有ScrollViewer.CanContentScroll
設置爲True
。一旦我在所有Child Grid
中將它設置爲False,它似乎神奇地工作。現在,當我摺疊我的行細節時,它會適當調整包含的行的大小。
爲我工作,thx – Artiom 2012-08-14 17:24:33