Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridCheckBoxColumn IsReadOnly property binding

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

Here's XAML where I tried a DataGridCheckBoxColumn-

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

Here's XAML where I tried a 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 figuring this out is much appreciated.

like image 908
Mark Libner Avatar asked Jun 18 '13 20:06

Mark Libner


1 Answers

First, there is no need to specify a CellEditingTemplate when just using CheckBoxes. CheckBoxes themself are "editable/checkable". So remove that CellEditingTemplate since this makes no sense.

Have you tried to bind the IsEnabled property of the CheckBox directly to your Enabled property of your AccountComponent in the CellTemplate (like you did it in the CellEditingTemplate)? This should solve your problem.

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Grid>
            <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}"
                      IsEnabled="{Binding Enabled}"/>
        </Grid>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
like image 190
Markus Avatar answered Oct 03 '22 14:10

Markus