WPF Datagrid RowDetailsTemplate 可见性绑定到属性

2024-03-30

我使用带有 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>

不幸的是,对于此应用程序来说,在“选定”行上显示行详细信息并不直观,客户端希望单击多行上的复选框来显示“行详细信息”窗格,同时还可以滚动网格选择其他行。换句话说,无论 DataGrid 上发生什么情况,都会修复显示 RowDetails 的行。

因此,当前滚动会关闭他们打开的 RowDetailsPanes。我想做的是在其中一列中有一个复选框,并将 RowDetails 面板可见性绑定到此属性,但我不知道如何做到这一点。问题很简单,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)
      {
      }
    }

我还没有探索过通过数据绑定来做到这一点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF Datagrid RowDetailsTemplate 可见性绑定到属性 的相关文章

随机推荐