本文介绍了WPF Datagrid的RowDetailsTemplate知名度必然属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的一个WPF DataGrid中一个RowDetails面板,其中RowDetailsVisibilityMode设置为VisibleWhenSelected和将SelectionMode =扩展,这样多行可以选择,因此显示RowDetails,如下:

I am using a WPF Datagrid with a RowDetails panel where the RowDetailsVisibilityMode is set to "VisibleWhenSelected" and the SelectionMode="Extended" so that multiple rows can be selected and hence display RowDetails, as below:

<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窗格中的复选框,同时也左右滚动网格选择其它行。换句话说修复显示RowDetails无论在DataGrid中发生的事情的记录。

Unfortunately, for this application it isn't intuitive to display row details on 'selected' rows, the client would like to click a checkbox on a number of rows to display the RowDetails pane, but also scroll around the grid selecting other rows. In other words fix the rows that display RowDetails no matter what happens on the DataGrid.

所以,当前的滚动左右关闭,他们已经打开了RowDetailsPanes。我想这样做是有在其中一列的复选框,并结合RowDetails面板能见度此属性,但我无法弄清楚如何做到这一点。这个问题很简单,就是RowDetailsPane只能对行选择在DataGrid(S) - 可以将它以某种方式扩展到对我的选择的属性操作?

So currently scrolling around closes the RowDetailsPanes that they have opened. What I would like to do is to have a checkbox in one of the columns and bind the RowDetails panel visibility to this property but I can't figure out how to do it. The problem is simply that RowDetailsPane only operates on the row selection(s) in the datagrid - can it be extended somehow to operate on a property of my choosing?

在此先感谢,请问

推荐答案

纵观WPF工具包的源$ C ​​$ c分别DataGridRow有DetailsVisibility属性。

Looking at the WPF toolkit source code each DataGridRow has a DetailsVisibility property.

我把在第一列(只是用于测试)的按钮。

I put a button (just for testing) in the first column.

&LT;工具箱:DataGridTemplateColumn&GT;
&NBSP;&NBSP; &LT;工具箱:DataGridTemplateColumn.CellTemplate&GT;
&NBSP;&NBSP;&NBSP;&NBSP; &LT;的DataTemplate&GT;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &LT;按钮X:名称=buttonDetailsCONTENT =你好ButtonBase.Click =Details_Click/&GT;
&NBSP;&NBSP;&NBSP;&NBSP; &LT; / DataTemplate中&GT;
&NBSP;&NBSP; &LT; /工具包:DataGridTemplateColumn.CellTemplate&GT;
&LT; /工具包:DataGridTemplateColumn&GT;

<toolkit:DataGridTemplateColumn>
   <toolkit:DataGridTemplateColumn.CellTemplate>
     <DataTemplate>
       <Button x:Name="buttonDetails" Content="Hello" ButtonBase.Click="Details_Click"/>
     </DataTemplate>
   </toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

在单击该按钮,找到点击的行和切换属性。

When the button is clicked, find the clicked row and toggle the property.

   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)
      {
      }
    }

我还没有探索通过绑定这样做。

I have not explored doing this via databinding.

这篇关于WPF Datagrid的RowDetailsTemplate知名度必然属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 07:37