c# - 绑定(bind)到属性的 WPF Datagrid RowDetailsTemplate 可见性

标签 c# .net wpf datagrid wpfdatagrid

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

相关文章:

c# - 无法从文件连接到数据库

c# - 将一个对象分配给同一类的另一个对象

c# - 如何在 c# 中减少这个 IF-Else 阶梯

.net - SPRING.Net 是面向方面编程(AOP)的最佳框架吗?

c# - ItemsControl 中 WPF 中的可拖动对象?

c# - WPF TextBlock 重叠椭圆

c# - 如何在运行时执行 RowClick (ExtNet Store)

c# - 比较 MbUnit 3.1 中的 2 个列表

c# - 我应该用 TraceSource.TraceEvent 方法中的 id 参数来识别什么?

wpf - 密码框上的样式触发器