C# 基于单元格值的Datagrid单元格颜色

C# 基于单元格值的Datagrid单元格颜色,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个DataGrid和itemsourceDateTableDTableDay来自viewmodel。在DTableDay中,单元格的内容为空或内容为“1”。我想为内容为“1”的单元格设置绿色 我的xaml看起来像这样 但是如果我运行我的应用程序,它会抛出一个异常 “在使用ItemsSource时,操作无效。访问和 改为使用ItemsControl.ItemsSource修改元素。“ 有人能帮我吗?感谢一些问题,首先“DataGridCell”必须用DataGrid(即“DataGrid

我有一个
DataGrid
itemsource
DateTable
DTableDay
来自
viewmodel
。在
DTableDay
中,单元格的内容为空或内容为“1”。我想为内容为“1”的单元格设置绿色

我的xaml看起来像这样


但是如果我运行我的应用程序,它会抛出一个异常

“在使用ItemsSource时,操作无效。访问和 改为使用ItemsControl.ItemsSource修改元素。“

有人能帮我吗?感谢一些问题,首先“DataGridCell”必须用DataGrid(即“DataGrid.DataGridCell”)标识,否则DataGrid中的任何其他元素都不能以DataGrid作为前缀。将被解释为项,这意味着无法再次绑定ItemsSource。其次,要设置单元样式,请使用属性

<DataGrid ItemsSource="{Binding DTableDay}" AutoGenerateColumns="True" >
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
                <!-- your style -->
        </Style>
    </DataGrid.CellStyle>
 <DataGrid />

一些问题,首先“DataGridCell”必须用DataGrid(即“DataGrid.DataGridCell”)标识,否则DataGrid中的任何其他元素都不能以DataGrid作为前缀。将被解释为项,这意味着无法再次绑定ItemsSource。其次,要设置单元样式,请使用属性

<DataGrid ItemsSource="{Binding DTableDay}" AutoGenerateColumns="True" >
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
                <!-- your style -->
        </Style>
    </DataGrid.CellStyle>
 <DataGrid />

您可以定义自己的
DataGridTemplateColumn
。在此列中,您可以使用简单的
TextBlock
创建一个新的
DataTemplate
。将
TextBlock
Text
-属性绑定到
DTableDay
集合中对象的属性。(在下面的示例中,我假设绑定对象的属性名称为
“CellValue”
),然后根据
TextBlock
Text
属性创建
触发器

<DataGrid ItemsSource="{Binding DTableDay}" AutoGenerateColumns="False">
  <DataGrid.Columns>
    <DataGridTemplateColumn>
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding CellValue}">
            <TextBlock.Style>
              <Style TargetType="TextBlock">
                <Style.Triggers>
                  <Trigger Property="Text" Value="1">
                    <Setter Property="Background" Value="Green"/>
                  </Trigger>
                </Style.Triggers>
              </Style>
            </TextBlock.Style>
          </TextBlock>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>  
</DataGrid>

您可以定义自己的
DataGridTemplateColumn
。在此列中,您可以使用简单的
TextBlock
创建一个新的
DataTemplate
。将
TextBlock
Text
-属性绑定到
DTableDay
集合中对象的属性。(在下面的示例中,我假设绑定对象的属性名称为
“CellValue”
),然后根据
TextBlock
Text
属性创建
触发器

<DataGrid ItemsSource="{Binding DTableDay}" AutoGenerateColumns="False">
  <DataGrid.Columns>
    <DataGridTemplateColumn>
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding CellValue}">
            <TextBlock.Style>
              <Style TargetType="TextBlock">
                <Style.Triggers>
                  <Trigger Property="Text" Value="1">
                    <Setter Property="Background" Value="Green"/>
                  </Trigger>
                </Style.Triggers>
              </Style>
            </TextBlock.Style>
          </TextBlock>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>  
</DataGrid>


看起来您正在正确更改单元格颜色。您看到的异常可能是您在代码的另一部分中执行的操作的结果。你能看看堆栈跟踪,看看它是从哪里抛出的吗?看起来你正在正确地改变单元格的颜色。您看到的异常可能是您在代码的另一部分中执行的操作的结果。您可以查看堆栈跟踪并查看它从何处抛出的方法吗?