如何在属性更改时停止DataGrid折叠RowDetails
本文关键字:DataGrid 折叠 RowDetails 属性 | 更新日期: 2023-09-27 18:26:30
给定一个带有切换行详细信息按钮的DataGrid。为什么每当属性更改时,它都会折叠细节?
每当列使用时,它都会折叠详细信息
UpdateSourceTrigger=LostFocus
此外,每当在详细信息视图中更新特性时,它都会将其折叠。
有没有办法让它一直开着?该行仍处于选中状态。
XAML:
RowDetailsVisibilityMode="Collapsed"
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="buttonDetails" Content="Hello" ButtonBase.Click="Details_Click"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
C#:
// Load stuff from db
_context.JobCollection.Load();
// Set source with db stuff
jobViewSource.Source = _context.JobCollection.Local;
private void Details_Click(object sender, RoutedEventArgs e)
{
try
{
// the original source is what was clicked. For example
// a button.
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree upwards looking for
// the clicked row.
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
// if we found the clicked row
if (dep != null && dep is DataGridRow)
{
// get the row
DataGridRow row = (DataGridRow)dep;
// change the details visibility
if (row.DetailsVisibility == Visibility.Collapsed)
{
row.DetailsVisibility = Visibility.Visible;
}
else
{
row.DetailsVisibility = Visibility.Collapsed;
}
}
}
catch (System.Exception)
{
}
}
很少建议更改:
-
将
RowDetailsVisibilityMode="Collapsed"
应用于DataGrid。 -
你的按钮点击应该看起来像这样:
private void Details_Click(object sender, RoutedEventArgs e) { DataGridRow row = (DataGridRow)Dgrd1.ItemContainerGenerator.ContainerFromItem(Dgrd1.SelectedItem); if (row.DetailsVisibility == System.Windows.Visibility.Visible) row.DetailsVisibility = System.Windows.Visibility.Collapsed; else row.DetailsVisibility = System.Windows.Visibility.Visible; }