gpt4 book ai didi

wpf - 通过 ViewModel 禁用特定的 DataGridCheckBoxColumn 单元格

转载 作者:行者123 更新时间:2023-12-04 14:42:36 25 4
gpt4 key购买 nike

我的 MVVM 项目有这个 ViewModel:

class ListViewModel : ViewModelBase {
public ObservableCollection<ListItemviewModel> Items { ... }
}
class ListItemViewModel : ViewModelBase {
public String Name { ... }
public Boolean IsChecked { ... }
public Boolean IsEnabled { ... }
}

编写 XAML 似乎很简单:
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridCheckBoxColumn Header="Is checked" Binding="{Binding IsChecked}" />
</DataGrid>

但是,当 ListItemViewModelIsEnabled属性是 false DataGridCheckBoxColumn该行中的单元格被禁用?

我尝试设置 IsReadOnly={Binding IsDisabled} (并将 IsDisabled 属性添加到 ListItemViewModel ,但无济于事)-我认识到这将禁用/启用整个列,而不是单个单元格。

我还尝试了这些说明( How to disable a cell in a DataGrid while binding to an ObservableCollection):
<DataGridCheckBoxColumn Header="Is checked" Binding="{Binding IsChecked}" />
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.CellStyle>

但是,这没有任何效果,并且“输出”窗口中不会显示任何绑定(bind)错误。

最佳答案

事实证明,我链接到的问题 (How to disable a cell in a DataGrid while binding to an ObservableCollection) 几乎是正确的,但 XAML 有点偏爱 cargo 。正确的 XAML 仅仅是:

<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
</Style>
</DataGridCheckBoxColumn.CellStyle>
排序!

关于wpf - 通过 ViewModel 禁用特定的 DataGridCheckBoxColumn 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33366985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com