我正在使用带有 RowDetails 面板的 WPF Datagrid,其中 RowDetailsVisibilityMode 设置为“VisibleWhenSelected”和 SelectionMode="Extended",以便可以选择多行并因此显示 RowDetails,如下所示:
<dg:DataGrid x:Name="MyGrid"
ItemsSource="{Binding Path=MyItems}"
AutoGenerateColumns="True"
SelectionMode="Extended"
RowDetailsVisibilityMode="VisibleWhenSelected">
<dg:DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="Further Details..."/>
</DataTemplate>
</dg:DataGrid.RowDetailsTemplate>
...
</dg:DataGrid>
不幸的是,对于此应用程序,显示“选定”行上的行详细信息并不直观,客户端希望单击多个行上的复选框以显示 RowDetails Pane ,但也可以滚动网格选择其他行。换句话说,无论 DataGrid 上发生什么,都修复显示 RowDetails 的行。
因此当前滚动会关闭他们打开的 RowDetailsPanes。我想做的是在其中一列中有一个复选框,并将 RowDetails 面板的可见性绑定(bind)到该属性,但我不知道该怎么做。问题很简单,RowDetailsPane 只对数据网格中的行选择进行操作——它可以以某种方式扩展以对我选择的属性进行操作吗?
提前致谢, 将
最佳答案
查看 WPF 工具包源代码,每个 DataGridRow 都有一个 DetailsVisibility 属性。
我在第一列放了一个按钮(只是为了测试)。
<toolkit:DataGridTemplateColumn>
<toolkit:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="buttonDetails" Content="Hello" ButtonBase.Click="Details_Click" />
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>
当按钮被点击时,找到被点击的行并切换属性。
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)
{
}
}
我还没有探索过通过数据绑定(bind)来做到这一点。
关于c# - 绑定(bind)到属性的 WPF Datagrid RowDetailsTemplate 可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1471534/