-1

silverlight 的元素中必须有一个验证:如果在组合框中选择了特定值,则必须禁用DataGrid同一行 ( ) 上的复选框列。DataGridCheckBoxColumn但是,据我所见,我只能禁用整个列,这是不可接受的。

那么,如果我有坐标(行、列),如何禁用特定的单元格?

PS 类似的问题 -这里不适合我。silverlight 4中没有 CellEnter 和 CellLeave 事件

4

1 回答 1

2

部分答案可能是:

private void DisableCheckboxColumnInRow(DataGridRow row)
{
    var checkBoxColumn = Datagrid1.Columns[0];
    var checkBoxCell = GetCell(checkBoxColumn, row);
    checkBoxCell.IsEnabled = false;
}

private static DataGridCell GetCell(DataGridColumn column, DataGridRow row)
{
    var cellContent = column.GetCellContent(row);
    return (DataGridCell)cellContent.Parent;
}

但是我可能有更好的方法来使用 MVVM 做到这一点:

视图模型部分:

public class TestItem : INotifyPropertyChanged
{
    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
            OnPropertyChanged("IsChecked");
        }
    }

    private bool _canBeChecked;
    public bool CanBeChecked
    {
        get { return _canBeChecked; }
        private set
        {
            _canBeChecked = value;
            OnPropertyChanged("CanBeChecked");
        }
    }

    private string _selectedValue;
    public string SelectedValue
    {
        get { return _selectedValue; }
        set
        {
            _selectedValue = value;
            OnPropertyChanged("SelectedValue");
            // here we do the 'magic':
            CanBeChecked = SelectedValue != "one";
        }
    }

    public IEnumerable<string> PossibleValues
    {
        get
        {
            yield return "one";
            yield return "two";
            yield return "free";
        }
    }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

查看部分:

<Controls:DataGridTemplateColumn>
    <Controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding PossibleValues}" SelectedItem="{Binding SelectedValue,Mode=TwoWay}" />
        </DataTemplate>
    </Controls:DataGridTemplateColumn.CellTemplate>
</Controls:DataGridTemplateColumn>
<Controls:DataGridTemplateColumn>
    <Controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}" IsEnabled="{Binding CanBeChecked}" />
        </DataTemplate>
    </Controls:DataGridTemplateColumn.CellTemplate>
</Controls:DataGridTemplateColumn>
于 2012-12-07T13:36:27.510 回答