2012-06-18 23 views
0

我有一個DataGrid貨件和產品。總是顯示出貨量,並且每次出貨的產品都顯示在一個RowDetails中,當我雙擊一行時,這些產品就會顯示出來。檢查DataGrid中的CheckBox應該檢查RowDetails中的所有複選框

在DataGrid中我使用的是自定義的複選框列:

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

<DataGrid.RowDetailsTemplate>具有相同的列。 我在找的是檢查「主」複選框被選中時詳情視圖中的所有項目。

我有一個Shipment類和一個Product類。這兩個類都有DoCopy屬性。 裝運:

Run through all products and set DoCopy = true 

問題:

當我點擊DataGrid中的一個複選框,所有產品複選框被選中。但只有在RowDetails沒有顯示的情況下。如果顯示RowDetails並單擊「主」複選框,它將被檢查,但細節複選框不會。

此外,如果我以前已打開和關閉一行的細節,然後檢查「主」複選框,同樣的情況。產品的複選框保持未選中狀態。

裝運有一個List<Product>其中包含該貨件的所有產品。

任何想法?

+0

也許這可以幫助你。 http://stackoverflow.com/questions/6112857/handling-exclusive-or-conditions-when-binding-to-two-boolean-properties-in-xaml –

+0

Post the RowDetails – Paparazzi

回答

0

我知道了,夥計們。 我似乎忘了實施INotifyPropertyChanged。這一切都像現在這樣。謝謝:-)

0

thakrage, 來處理這個問題,使用點擊事件的每一個「副本」複選框行,在這種情況下,你可以設置Docopy = true或你喜歡做watever最簡單的方法...

然後定義在數據網格之外複選框,然後設置頁邊距以將複選框與數據頁眉完全相同,並通過點擊事件來檢查所有行。

參考我下面的示例代碼:

<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 
     } 
    } 
2

以下代碼適用於我。我只想選擇某個事件上的數據網格的所有複選框。下面的代碼簡單地檢查了數據網格中的所有複選框。在我的情況列零是一個複選框列

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; 
      } 
     } 
    } 
相關問題