如何以编程方式禁用数据网格WPF中的特定单元格 [英] How to disable a particular cell in a datagrid WPF programmatically

查看:77
本文介绍了如何以编程方式禁用数据网格WPF中的特定单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了禁用值为ONE的列中所有单元格的问题,我能够从datagrid读取字符串,但无法禁用该特定单元格。如果有解决方案,请告诉我。



我有完整的行和列信息,我有值,但无法禁用电池。



提前致谢



我尝试了什么:



我试图通过行或列,因为我们在datagridview中没有这里工作。

I am facing an issue with disabling all cells in a column having value "ONE", I am able to read the string from datagrid but am unable to make that particaular cell disabled. Please let me know if there is a solution for it.

I am having entire row and column info where i am having the values, but am unable to disable that exact cell.

Thanks in advance

What I have tried:

I tried to get through the rows or columns as we do in datagridview was not working here.

推荐答案

好的,这是你怎么做的。假设您有以下模型:
Okay, here's how you do this. Suppose you have the following model:
public class Data
{
  public bool IsChecked { get; set; }
  public string Value { get; set; }
}

现在,这会在ViewModel中暴露给DataGrid,如下所示:

Now, this is exposed to the DataGrid in a ViewModel like this:

public ObservableCollection<Data> Items { get; } = new ObservableCollection<Data>();

private void Populate()
{
  Items.Add(new Data() { IsChecked = true, Value = "One" });
  Items.Add(new Data() { IsChecked = true, Value = "One" });
  Items.Add(new Data() { IsChecked = true, Value = "Two" });
  Items.Add(new Data() { IsChecked = true, Value = "One" });
  Items.Add(new Data() { IsChecked = true, Value = "Three" });
  Items.Add(new Data() { IsChecked = true, Value = "One" });
  Items.Add(new Data() { IsChecked = true, Value = "One" });
}

这一切都非常简单。现在,拼图的最后一部分是我们创建 DataGrid 的方式。具体来说,我们将创建一个 CheckBoxStyle 样式,我们将 ElementStyle 用作 DataGridCheckBoxColumn 并且该样式将具有 DataTrigger ,它对我们的我们的数据模型中的属性。因此,我们最终得到了这个XAML

That's all very straightforward. Now, the final piece of the puzzle is the way we create our DataGrid. Specifically, we're going to create a CheckBoxStyle style that we use as our ElementStyle for the DataGridCheckBoxColumn and that style is going to have a DataTrigger that reacts to the contents of our Value property in our Data model. So, we end up with this XAML

<DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
  <DataGrid.Resources>
    <Style TargetType="{x:Type CheckBox}" x:Key="CheckBoxStyle">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Value}" Value="One">
          <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </DataGrid.Resources>
  <DataGrid.Columns>
    <DataGridCheckBoxColumn Binding="{Binding IsChecked}" ElementStyle="{StaticResource CheckBoxStyle}" />
    <DataGridTextColumn Binding="{Binding Value}" />
  </DataGrid.Columns>
</DataGrid>

就这么简单。


这篇关于如何以编程方式禁用数据网格WPF中的特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆