使用CanUserAddRows为DataGrid设置样式化新行

时间:2015-04-09 05:40:46

标签: c# wpf xaml

我需要为DataGrid CanUserAddRows中的设置样式。 我ItemsSource使用DataGrid 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}}

新行不是番茄颜色=( enter image description here

1 个答案:

答案 0 :(得分:2)

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

需要 .NET FrameWork 4.5

设置 Trigger.Property = IsNewItem Trigger.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>