2015-11-17 138 views
0

該任務如下:有一個帶有ComboboxColumns的DataGrid。如果用戶更改單元格[2,3]並選擇值!= 0,則禁用單元格[3,2]。我寫了下面的處理程序:DataGridComboboxColumn - 獲取單元格值

private void grAssessment_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
    { 
     int x = e.Column.DisplayIndex; 
     int y = e.Row.GetIndex();   

     grAssessment.GetCell(x, y).IsEnabled = false; 
     grAssessment.GetCell(x, y).Background = Brushes.LightGray; 
    } 

但它禁用適當的單元格。如何將selected value!=0條件添加到此代碼?

在此先感謝。

+0

嗨,這不是那麼清楚。你能再解釋一遍嗎? – Ilan

+0

@llan,每個下拉列表的值爲{0,1,2,3,4,5}。如果用戶編輯網格中的單元格[rowIndex,columnIndex]並選擇了值!= 0,則應禁用單元格[columnIndex,rowIndex] – Irina

回答

0

我發現下面的解決方案

private void grAssessment_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
{ 
    int x = e.Column.DisplayIndex; 
    int y = e.Row.GetIndex(); 
    DataGridCell cell = grAssessment.GetCell(y, x); 
    if (((System.Windows.Controls.ComboBox)(cell.Content)).Text != "") 
    { 
     grAssessment.GetCell(x, y).IsEnabled = false; 
     grAssessment.GetCell(x, y).Background = Brushes.LightGray; 
    } 
    else 
    { 
     grAssessment.GetCell(x, y).IsEnabled = true; 
     grAssessment.GetCell(x, y).Background = Brushes.White; 
    }    
} 
0

嘗試檢查了這一點: 1. DataGridComboBoxColumn的ItemsSource(當前版本只,用你自己的):

private ObservableCollection<ScoreData> _scores = new ObservableCollection<ScoreData>(

    new List<ScoreData> 
    { 
     new ScoreData{Score = 1, ScoreVerbal = "the same"}, 
     new ScoreData{Score = 3, ScoreVerbal = "moderate superiority"}, 
     new ScoreData{Score = 5, ScoreVerbal = "strong superiority"}, 
     new ScoreData{Score = 7, ScoreVerbal = "the samvery strong superioritye"}, 
     new ScoreData{Score = 9, ScoreVerbal = "extremely superiority"}, 
    }); 

2.處理程序代碼(在數據網格中定義的處理程序):

private void SelectDataGrid_OnCellEditEnding(object sender, 
     DataGridCellEditEndingEventArgs e) 
    { 
     var editingElement = e.EditingElement as Selector; 
     if(editingElement == null) return; 
     var selectedData = editingElement.SelectedItem as ScoreData; 
     if(selectedData == null || selectedData.Score > 1) return; 
     var dataGridCell = editingElement.GetVisualParentOfType<DataGridCell>(); 
     if(dataGridCell == null) return; 
     dataGridCell.IsEnabled = false; 
    } 

3. GetVisualParentOfType代碼(如擴展類的一部分):

public static class VisualTreeExtensions 
{ 
    public static T GetVisualParentOfType<T>(this DependencyObject child) 
     where T : DependencyObject 
    { 
     if (child is T) 
      return child as T; 
     var parentObject = VisualTreeHelper.GetParent(child); 
     if (parentObject == null) return null; 
     var parent = parentObject as T; 
     return parent ?? GetVisualParentOfType<T>(parentObject); 
    } 

    public static T GetChildOfType<T>(this DependencyObject depObj) 
    where T : DependencyObject 
    { 
     if (depObj == null) return null; 

     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 
     { 
      var child = VisualTreeHelper.GetChild(depObj, i); 

      var result = (child as T) ?? GetChildOfType<T>(child); 
      if (result != null) return result; 
     } 
     return null; 
    } 
} 

個問候,

+0

@llan,但不幸的是,它不能以我描述的方式工作 – Irina