【问题标题】:Checking a CheckBox in DataGrid should Check all checkboxes in RowDetails检查 DataGrid 中的 CheckBox 应检查 RowDetails 中的所有复选框
【发布时间】:2012-06-20 08:08:36
【问题描述】:

我有一个 DataGrid Shipments and Products。始终显示发货,每个发货的产品都显示在 RowDetails 中,当我双击一行时,它会变得可见。

在 DataGrid 中,我使用的是自定义复选框列:

<DataGridTemplateColumn>
   <DataGridTemplateColumn.Header>
       Copy
   </DataGridTemplateColumn.Header>
   <DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
           <CheckBox IsChecked="{Binding Path=DoCopy, Mode=TwoWay, 
             UpdateSourceTrigger=PropertyChanged}"
       </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我的&lt;DataGrid.RowDetailsTemplate&gt; 有相同的列。 我正在寻找的是在选中“主要”复选框时检查详细信息视图中的所有项目。

我有一个 Shipment 类和一个 Product 类。这两个类都有 DoCopy 属性。 发货:

Run through all products and set DoCopy = true

问题:

当我单击 DataGrid 中的复选框时,所有产品的复选框都会被选中。但仅当未显示 RowDetails 时。如果显示 RowDetails 并且我单击“主”复选框,则会选中它,但不会选中详细信息复选框。

此外,如果我之前打开和关闭了行详细信息,然后选中“主”复选框,也会发生同样的情况。产品的复选框保持未选中状态。

货件有一个List&lt;Product&gt;,其中包含该货件的所有产品。

有什么想法吗?

【问题讨论】:

标签: c# wpf datagrid


【解决方案1】:

以下代码对我有用。我只是想在某个事件上选中数据网格的所有复选框。以下代码只是检查了数据网格中的所有复选框。在我的情况下,第 0 列是一个复选框列

private void SelectAll()
    {
        for (int i = 0; i < dgVehicle.Items.Count; i++)
        {
            DataGridRow row = (DataGridRow)dgVehicle.ItemContainerGenerator.ContainerFromIndex(i);

            if (row != null)
            {
                CheckBox chk = dgVehicle.Columns[0].GetCellContent(row) as CheckBox;
                chk.IsChecked = true;
            }
        }
    } 

【讨论】:

    【解决方案2】:

    我明白了,伙计们。 我好像忘了实现INotifyPropertyChanged。 现在一切正常。谢谢:-)

    【讨论】:

      【解决方案3】:

      thkrage, 处理此问题的最简单方法是为每个“复制”复选框行使用单击事件,在这种情况下,您可以设置 Docopy=true 或您喜欢做的任何事情...

      然后在数据网格之外定义一个复选框,然后设置边距以将复选框与数据标题完全相同,并冒泡一个单击事件以检查所有行。

      请参考下面的示例代码:

      <CheckBox Name="chkbox_chkall" Click="chkbox_chkall_Click" Content="Check all" BorderBrush="#FF828282" Foreground="#FF5B585A"/>
      

      <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <CheckBox IsChecked="{Binding Path=DoCopy, Mode=TwoWay, 
               UpdateSourceTrigger=PropertyChanged}" Click="chkBoxRow_Click"
         </DataTemplate>
      

      在代码隐藏中:

          private void chkbox_chkall_Click(object sender, RoutedEventArgs e)
          {
              CheckBox chkbox_chkall = sender as CheckBox;
              foreach (OnlineActivatedProducts rowItem in (grdProducts.ItemsSource))
              {
                  CheckBox chk = grdProducts.Columns[6].GetCellContent(rowItem) as CheckBox;
                  if (chkbox_chkall.IsChecked == true)
                  {
                      chk.IsChecked = true;
                  }
                  else
                  {
                      chk.IsChecked = false;
                  }
                  chkBoxRow_Click(chk, e); // which bubbles each rows checked / unchecked event
              }
          } 
      
          private void chkBoxRow_Click(object sender, RoutedEventArgs e)
          {
              if (chkBoxContent.IsChecked.Value)
              {
                  //if checked do something here 
              }
              else if (!chkBoxContent.IsChecked.Value)
              {
                  //if unchecked do something here
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-10
        • 1970-01-01
        • 1970-01-01
        • 2012-04-10
        • 1970-01-01
        • 1970-01-01
        • 2020-03-29
        相关资源
        最近更新 更多