Associating DataGridCheckBoxColumn IsReadOnly Properties

I want the checkbox column in my datagrid on / off for each row depending on the value in the collection. I have an ObservableCollection called AccountComponents, which is a collection of the AccountComponent class that has the boolean property Enabled. I tried binding the Enabled property to IsReadOnly and IsEnabled with no luck.

Here's the XAML where I tried DataGridCheckBoxColumn -

<DataGridCheckBoxColumn Binding="{Binding IsChecked}" IsReadOnly="{Binding AccountComponents/Enabled}"/> 

Here's the XAML where I tried the DataGridTemplateColumn -

 <DataGridTemplateColumn Header=""> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}" IsEnabled="False"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <Grid> <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}" IsEnabled="{Binding Enabled}"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> 

Any help on this understanding is greatly appreciated.

+6
source share
1 answer

First, there is no need to specify a CellEditingTemplate when using CheckBoxes. CheckBoxes themselves are "editable / verifiable." Therefore, delete this CellEditingTemplate , as this does not make sense.

Have you tried to bind the IsEnabled CheckBox property directly to the Enabled property of your AccountComponent in a CellTemplate (for example, did you do this in a CellEditingTemplate)? This should solve your problem.

 <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid> <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}" IsEnabled="{Binding Enabled}"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> 
+5
source

Source: https://habr.com/ru/post/947594/


All Articles