Jean Tehhe
Jean Tehhe

Reputation: 1357

Data grid checkbox disable un-checked option for key

I'm using DataGrid and I have one column with aCheckbox. I would like that whenever a row has the key, the checkbox will be disabled so as to the user cannot uncheck it. I have a property in my view model which is called IsKey with INotifyPropertyChanged. How can I do that?

the IsChecked binding is that all the Checkboxes are checked when starting the DataGrid and user can uncheck rows, besides keys...

<DataGridTemplateColumn.CellTemplate>
   <DataTemplate>
      <CheckBox x:Name="CheckBox" 
          IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"/>
   </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

Upvotes: 0

Views: 119

Answers (1)

dkozl
dkozl

Reputation: 33384

Assuming that IsKey is bool you can use Style.Triggers so when IsKey is true set IsEnabled to false:

<CheckBox x:Name="CheckBox" IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}">
    <CheckBox.Style>
        <Style TargetType="{x:Type CheckBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsKey}" Value="True">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

Upvotes: 1

Related Questions