c# - 更改 DataGridTemplateColumn 内容样式

标签 c# css wpf

我想改变DataGridTemplateColumn的内容样式,这里是DataGridTemplateColumn的代码:

<DataGridTemplateColumn MaxWidth="50" MinWidth="30" CellStyle="{StaticResource CellStyle}">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <Label Style="{StaticResource FontAwesome}" Padding="0" VerticalAlignment="Center" HorizontalAlignment="Center" Cursor="Hand" MouseLeftButtonDown="LblEdit_MouseLeftButtonDown">
            
        </Label>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

这是 CellStype 代码:

<Style x:Key="CellStyle" TargetType="DataGridCell">
    <Setter Property="BorderThickness" Value="0 0 1 0"/>
    <Setter Property="BorderBrush" Value="#F6F6F6"/>
    <Setter Property="Foreground" Value="#000"/>
    <Setter Property="FontSize" Value="14" />
    <Style.Triggers>
        <Trigger Property="DataGridCell.IsSelected" Value="True">
            <Setter Property="Background" Value="#FF5750" />
            <Setter Property="Foreground" Value="#FFFFFF" />
        </Trigger>
    </Style.Triggers>
</Style>

enter image description here

但是当选择行时 DataTemplate 中的标签不起作用,尽管其他单元格有效。

有什么办法吗?

谢谢!

最佳答案

触发器设置 DataGridCell 的前景色,但不设置 Label 的前景色。您可以通过向其添加此 Foreground 属性来引用 Label 中的前景色:

<Label 
    Style="{StaticResource FontAwesome}" 
    Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Control}, Path=Foreground}"
    ... />

关于c# - 更改 DataGridTemplateColumn 内容样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27165684/

相关文章:

以 I 开头的 C# 类名

html - 使用链接中的完整性参数,导航栏无法在 Bootstrap 中工作

css - 在不重新加载页面的情况下重新加载 CSS 样式表?

c# - 如何使用 LINQ 在 ComboBox 中显示不同的属性值?

c# - 绑定(bind)到 UserControl 中的属性

c# - 如何加速 .NET 客户端应用程序(Windows 窗体或 WPF)的初始化?

c# - 神秘的 System.Object.GetType() NullReferenceException

c# - 将代码从 SVN 导入 GIT 失败 - SVN trunk 为空

css - 如何将div中的文本居中

c# - 为 asp.net MVC 静态资源设置合成路由