【问题标题】:Disable specific DataGridCheckBoxColumn cells via ViewModel通过 ViewModel 禁用特定的 DataGridCheckBoxColumn 单元格
【发布时间】: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>

但是,当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>

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

【问题讨论】:

    标签: wpf mvvm datagrid


    【解决方案1】:

    事实证明,我链接到的问题 (How to disable a cell in a DataGrid while binding to an ObservableCollection) 几乎是正确的,但 XAML 有点像货物崇拜。正确的 XAML 只是:

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

    排序!

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2012-05-16
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多