7

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.

4

1 回答 1

5

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>
于 2013-06-19T08:47:14.473 回答