【发布时间】:2016-01-26 19:20:31
【问题描述】:
我的 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>
但是,当ListItemViewModel 的IsEnabled 属性为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>
但是这没有任何效果,并且输出窗口中不会显示任何绑定错误。
【问题讨论】: