不显示ControlTemplate不透明度,就像我想要的那样 [英] ControlTemplate Opacity not displayed like I want it to

查看:84
本文介绍了不显示ControlTemplate不透明度,就像我想要的那样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为DataGridCell

<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

During Runtime I add additional Triggers for specific Columns like this in my Code

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的地方,则颜色无法正确显示.

But if I focus one of the Cells where a DataTrigger I added in my Code is triggerd the Color isn't displayed correctly.

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

How can I display the fuchsia Sides in front of the Gold Background?

关注灰色细胞:

关注金电池:

更新

<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%的金色着色

Your problem might be, that you apply a golden color brush above the fuchsia edges, so they get tinted by 50% of gold.

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

此外,您还应该知道,在XAML中使用Transparent表示没有不透明的白色",因此,如果要淡化色粉刺,则不应淡化为透明的白色,而应淡化为透明的紫红色.

Additionally you should be aware that using Transparent in XAML means "white with no opacity", so if you want to fade out funchsia you should not fade to transparent white, but to transparent fuchsia instead.

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

要强制执行z顺序,您可以在给定的项目周围使用其他容器,并在运行期间"中应用状态.无论如何,这不是MVVM方法.

To enforce the z-order you could use an additional container around the item given and apply the status here "during runtime". Anyway this will not be an MVVM approach.

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

The better way might be to define a custom template-parameter (bool IsSpecificColumn) in your XAML, which will be set in your code "during runtime". Using MultiTrigger you can also define a third state for gold+fuchsia.

如果必须组合更多状态,则应让WPF来完成这项工作,并将上述方法与上述带盒装的容器以及模板参数一起使用.

If you have to combine even more states you should let WPF do the job and use the approach with the boxed containers mentioned above together with template-parameters.

这篇关于不显示ControlTemplate不透明度,就像我想要的那样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆