c# - WPF DataGrid 中的自定义复选框不更新绑定(bind)

标签 c# wpf datagrid binding

我有以下(简化)样式:

<Style x:Key="MyStyle" TargetType="{x:Type CheckBox}">  
    <Setter Property="Background" Value="Blue" />  
</Style>

如果我将它用作我的 DataGridCheckBoxColumn 中的 ElementStyle 和 EditingElementStyle:

<DataGridCheckBoxColumn Binding="{Binding IsEnabled}"  
                        ElementStyle="{StaticResource MyStyle}"  
                        EditingElementStyle="{StaticResource MyStyle}" />

然后,当我选中/取消选中某行的复选框时,我的绑定(bind) IsEnabled 不会切换。如果我删除 ElementStyle、EditingElementStyle 或两者,则绑定(bind)更新没有问题。这是为什么?!

此外,我尝试使用以下代码解决该问题:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsEnabled}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

但是,问题依然存在。

最佳答案

抱歉死机了,但我想我在 Stack Overflow 上找到了一个更好的解决方案,它可能会帮助那些最终在这个页面上搜索解决方案的人。

https://stackoverflow.com/a/7270548/3082531

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

我试过了,它对我来说非常有效,比公认的解决方案更简单,而且无需额外点击复选框。

关于c# - WPF DataGrid 中的自定义复选框不更新绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2926533/

相关文章:

silverlight - 验证 Silverlight DataGrid 中的新行

c# - VB.Net Power 运算符 (^) 从 C# 重载

c# - 在实现具有同步和异步 API 的库以实现相同功能时使用 async await

c# - 从不使用继承的两个类(具有相同的属性名称)获取属性值的优雅解决方案是什么?

c# - 在开头引号旁边使用大括号时出现 HeaderStringFormat 问题

wpf - PowerShell WPF 数据网格 : Exception thrown committing empty row

c# - 从 DataGrid MVVM 中的 TextBox 读取文本(wpf 数据绑定(bind))

c# - 为派生类编写泛型方法

javascript - WPF WebBrowser - 在 JavaScript 中调用方法

c# - 通过 Adapter.Update 将 DataTable 保存到 SQLite 数据库