使用CanUserAddRows为DataGrid样式化新行

3

我需要为DataGrid中的一个进行样式设计,以适应CanUserAddRowsDataGridItemsSource使用DataTable.DefaultView。 我尝试了:

<DataGrid IsReadOnly="false" CanUserAddRows="True">
  <DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=.}" Value="{x:Null}">
          <Setter Property="Background" Value="Tomato"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </DataGrid.Resources>
  <DataGrid.Columns>
    <DataGridTextColumn Width="*" Binding='{Binding Path=Col1}' Header="Col1" />
    <DataGridTextColumn Width="*" Binding='{Binding Path=Col2}' Header="Col2" />
    <!-- more columns -->
  </DataGrid.Columns>
</DataGrid>

但是新行不是番茄颜色=(进入图像描述

1个回答

2

我找到了这个问题的答案!

需要.NET Framework 4.5

设置Trigger.Property=IsNewItemTrigger.Value=true

          <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
            <Style.Triggers>
              <Trigger Property="DataGridRow.IsNewItem" Value="true">
                <Setter Property="DataGridCell.Background" Value="Tomato"/>
              </Trigger>
            </Style.Triggers>
          </Style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接