ControlTemplate 不透明度没有像我想要的那样显示

ControlTemplate Opacity not displayed like I want it to

我为我的 DataGridCell

定义了一个 Style
<Style x:Key="MYDGCellStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Template" Value="{DynamicResource MYDGCellControlTemplate}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsReadOnly}" Value="True">
                <Setter Property="Background">
                    <Setter.Value>
                        <SolidColorBrush Color="Gray" Opacity="0.3" />
                    </Setter.Value>
                </Setter>
            </DataTrigger>                
        </Style.Triggers>
    </Style>

    <ControlTemplate x:Key="MYDGCellControlTemplate" TargetType="{x:Type DataGridCell}">
        <Grid x:Name="CellGrid">
            <Border Background="{TemplateBinding Background}">
                <ContentPresenter VerticalAlignment="Center" Margin="4,0,6,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
            </Border>                
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsFocused" Value="true">
                <Setter Property="Background" TargetName="CellGrid">
                    <Setter.Value>
                        <LinearGradientBrush Opacity="2" StartPoint="0,0.5" EndPoint="1,0.5">
                            <GradientStop Offset="0" Color="Fuchsia" />
                            <GradientStop Offset="0.2" Color="Transparent" />
                            <GradientStop Offset="0.8" Color="Transparent" />
                            <GradientStop Offset="1" Color="Fuchsia" />
                        </LinearGradientBrush>                            
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

在运行时,我在我的代码

中为特定的Columns添加了额外的Triggers
Style Sty = null;
if (Sty == null)
{
    Sty = new Style(typeof(DataGridCell));                                
    Sty.BasedOn = (Style)Application.Current.TryFindResource("MYDGCellStyle");                                
}

DataTrigger t = new DataTrigger();
t.Binding = new Binding("ProductionStatus");
t.Value = "I";
Setter s = new Setter();
s.Property = DataGridCell.BackgroundProperty;
s.Value = new SolidColorBrush(Colors.Gold) { Opacity = 0.5 };
t.Setters.Add(s);

Sty.Triggers.Add(t);
DGC.CellStyle = Sty;

但是,如果我关注 Cell 之一,其中触发了我在代码中添加的 DataTrigger,则颜色显示不正确。

如何在金色背景前显示紫红色边?

灰色细胞聚焦:

金细胞聚焦:

更新

<ControlTemplate x:Key="MYDGCellControlTemplate" TargetType="{x:Type DataGridCell}">            
            <Grid>
            <Grid Grid.ZIndex="99"  x:Name="CellGrid"/>
            <Border Grid.ZIndex="98" Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" Margin="4,0,6,0" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
            </Grid>            
        <ControlTemplate.Triggers>
            <Trigger Property="IsFocused" Value="true">
                <Setter Property="Background" TargetName="CellGrid">
                    <Setter.Value>
                        <LinearGradientBrush Opacity="2" StartPoint="0,0.5" EndPoint="1,0.5">
                            <GradientStop Offset="0" Color="Fuchsia" />
                            <GradientStop Offset="0.1" Color="#00FF00FF" />
                            <GradientStop Offset="0.9" Color="#00FF00FF" />
                            <GradientStop Offset="1" Color="Fuchsia" />
                        </LinearGradientBrush>                            
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

您的问题可能是,您在紫红色边缘上方 应用了金色画笔,因此它们被染上了 50% 的金色。

s.Value = new SolidColorBrush(Colors.Gold) { Opacity = 0.5 };

此外你应该知道在XAML中使用Transparent意味着"white with no opacity",所以如果你想淡出紫红色你不应该淡出透明白色,而是透明紫红色相反。

White:               #FFFFFFFF
Transparent:         #00FFFFFF
Fuchsia opaque:      #FFFF00FF
Fuchsia transparent: #00FF00FF

要强制执行 z 顺序,您可以在给定的项目周围使用额外的容器并在此处应用状态 "during runtime"。无论如何,这不是 MVVM 方法。

更好的方法可能是在您的 XAML 中定义自定义模板参数 (bool IsSpecificColumn),这将在您的代码 "during runtime" 中设置。使用 MultiTrigger 您还可以为金色+紫红色定义第三种状态。

如果您必须组合更多的状态,您应该让 WPF 来完成这项工作,并使用上述盒装容器和模板参数的方法。

尝试以您的新样式 (I ProductionStatus) 创建一个新的 "IsFocused" 触发器,如下所示:

<Trigger Property="IsFocused" Value="true">
    <Setter Property="Background" TargetName="CellGrid">
        <Setter.Value>
            <LinearGradientBrush Opacity="2" StartPoint="0,0.5" EndPoint="1,0.5">
                <GradientStop Offset="0" Color="Fuchsia" />
                <GradientStop Offset="0.2" Color="Gold" />
                <GradientStop Offset="0.8" Color="Gold" />
                <GradientStop Offset="1" Color="Fuchsia" />
             </LinearGradientBrush>                            
         </Setter.Value>
    </Setter>
</Trigger>

我知道你是在代码背后做的,但我想你会理解我的(更容易复制粘贴)。