DataGridComboboxColumn - 获取单元格值

时间:2015-11-17 05:07:15

标签: wpf combobox datagrid cell disabled-control

任务如下:有一个带有ComboboxColumns的DataGrid。如果用户更改单元格[2,3]并选择了值!= 0,则禁用单元格[3,2]。我写了以下处理程序:

    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条件?

提前致谢。

2 个答案:

答案 0 :(得分:0)

尝试检查一下:  1. DataGridComboBoxColumn ItemsSource(仅限当前版本,使用您自己的版本):

before_action :clear_admin_layout

2。处理程序代码(在数据网格上定义的处理程序):

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"},
    } );

3。 GetVisualParentOfType代码(作为扩展类的一部分):

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

的问候,

答案 1 :(得分: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; 
    }               
}