答えの一部は次のようになります。
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を使用してこれを行うより良い方法があるかもしれません:
ViewModel 部分:
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>