Okay, here's how you do this. Suppose you have the following model:
public class Data
{
public bool IsChecked { get; set; }
public string Value { get; set; }
}
Now, this is exposed to the DataGrid in a ViewModel like this:
public ObservableCollection<Data> Items { get; } = new ObservableCollection<Data>();
private void Populate()
{
Items.Add(new Data() { IsChecked = true, Value = "One" });
Items.Add(new Data() { IsChecked = true, Value = "One" });
Items.Add(new Data() { IsChecked = true, Value = "Two" });
Items.Add(new Data() { IsChecked = true, Value = "One" });
Items.Add(new Data() { IsChecked = true, Value = "Three" });
Items.Add(new Data() { IsChecked = true, Value = "One" });
Items.Add(new Data() { IsChecked = true, Value = "One" });
}
That's all very straightforward. Now, the final piece of the puzzle is the way we create our
DataGrid
. Specifically, we're going to create a
CheckBoxStyle
style that we use as our
ElementStyle
for the
DataGridCheckBoxColumn
and that style is going to have a
DataTrigger
that reacts to the contents of our
Value
property in our Data model. So, we end up with this XAML
<DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
<DataGrid.Resources>
<Style TargetType="{x:Type CheckBox}" x:Key="CheckBoxStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding Value}" Value="One">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsChecked}" ElementStyle="{StaticResource CheckBoxStyle}" />
<DataGridTextColumn Binding="{Binding Value}" />
</DataGrid.Columns>
</DataGrid>
It's that simple.