【发布时间】:2016-03-18 13:12:02
【问题描述】:
给定一个带有切换行详细信息按钮的 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)
{
}
}
【问题讨论】: