我的MVVM项目有这个ViewModel:
class ListViewModel : ViewModelBase {
public ObservableCollection<ListItemviewModel> Items { ... }
}
class ListItemViewModel : ViewModelBase {
public String Name { ... }
public Boolean IsChecked { ... }
public Boolean IsEnabled { ... }
}
Run Code Online (Sandbox Code Playgroud)
编写XAML似乎很简单:
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridCheckBoxColumn Header="Is checked" Binding="{Binding IsChecked}" />
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
但是,我怎么能得到这么当ListItemViewModel的IsEnabled财产是false在DataGridCheckBoxColumn"该行中的蜂窝被禁用?
我尝试设置IsReadOnly={Binding IsDisabled}(并添加IsDisabled属性ListItemViewModel,但无济于事) - 我认识到将禁用/启用整个列,而不是单个单元格.
我也试过这些指令(如何在绑定到ObservableCollection时禁用DataGrid中的单元格):
<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>
Run Code Online (Sandbox Code Playgroud)
但是这没有效果,并且"输出"窗口中不显示任何绑定错误.
事实证明我链接到的问题(如何在绑定到ObservableCollection时禁用DataGrid中的单元格)几乎是正确的,但XAML有点疯狂.正确的XAML仅仅是:
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
</Style>
Run Code Online (Sandbox Code Playgroud)
排序!
| 归档时间: |
|
| 查看次数: |
2188 次 |
| 最近记录: |