wpf DataGridCheckBoxColumn IsReadOnly 属性绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17178073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:01:47  来源:igfitidea点击:

DataGridCheckBoxColumn IsReadOnly property binding

wpfxamldata-bindingmvvm

提问by Mark Libner

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.

我希望根据集合中的值为每一行启用/禁用数据网格中的复选框列。我有一个名为 AccountComponents 的 ObservableCollection,它是一个名为 AccountComponent 的类的集合,它有一个名为 Enabled 的布尔属性。我试过将 Enabled 属性绑定到 IsReadOnly 和 IsEnabled ,但没有成功。

Here's XAML where I tried a DataGridCheckBoxColumn-

这是我尝试使用 DataGridCheckBoxColumn 的 XAML-

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

Here's XAML where I tried a DataGridTemplateColumn-

这是我尝试使用 DataGridTemplateColumn 的 XAML-

<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.

任何帮助解决这个问题都非常感谢。

采纳答案by Markus

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

首先,CellEditingTemplate只使用 CheckBoxes 时不需要指定 a 。复选框本身是“可编辑/可检查的”。所以删除它,CellEditingTemplate因为这没有意义。

Have you tried to bind the IsEnabledproperty of the CheckBox directly to your Enabledproperty of your AccountComponentin the CellTemplate (like you did it in the CellEditingTemplate)? This should solve your problem.

您是否尝试将IsEnabledCheckBox的属性直接绑定到EnabledAccountComponent在 CellTemplate 中的属性(就像您在 CellEditingTemplate 中所做的那样)?这应该可以解决您的问题。

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