c# - 使用 CanUserAddRows 为 DataGrid 的新行设置样式

标签 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>

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

最佳答案

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

需要.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>

关于c# - 使用 CanUserAddRows 为 DataGrid 的新行设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29530608/

相关文章:

xaml - mvvm 中的 3 状态复选框绑定(bind),无法设置空状态

c# - 使用 wpf 应用程序读取条形码

c# - 添加新的 CLR 用户定义函数时获取 "Cannot find the type nvarchar(MAX)"

c# - 性能调整分隔文件读取和写入数据库

wpf - 如何强制 TabItem 在加载时初始化内容?

.net - XAML 文件如何与 cs 文件关联?

c# - WebClient.DownloadString 不返回任何数据

WPF - 在代码中访问 XAML 资源

wpf - 无法在 WPF 中使用 MVVM 禁用控件(文本框)?

wpf - WPF 中的图案画笔