我可以访问 WPF 数据模板中的命名填充渐变吗?

发布于 2024-09-30 01:06:47 字数 3245 浏览 4 评论 0原文

我有一个包含许多分层文本和图形对象的数据模板。其中之一是来自矩形的 RadialGradientBrush Fill 属性的发光效果。首先,我将矩形命名为“矩形”并绑定到“填充”属性,并使用 DataTrigger 更改它。这工作得很好,但我在资源部分有许多 RadialGradientBrush 对象,正如您在下面看到的,当我想做的只是更改 GradientStops 时,需要重复很多工作。因此,我删除了 Fill 绑定并添加并命名为 RadialGradientBrush,虽然我可以从资源绑定到画笔,但无法在 DataTrigger 中访问它。我收到“找不到触发目标”错误。

<Rectangle x:Name="Glow" IsHitTestVisible="False" RadiusX="1.5" RadiusY="1.5" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" StrokeThickness="0" Opacity="1.0">
    <Rectangle.Fill>
        <RadialGradientBrush x:Name="GlowGradient" Center="0.5,0.848" GradientOrigin="0.5,0.818" RadiusX="-1.424" RadiusY="-0.622" GradientStops="{StaticResource DefaultGradient}">
            <RadialGradientBrush.RelativeTransform>
                <TransformGroup>
                    <ScaleTransform CenterX="0.5" CenterY="0.848" ScaleX="1" ScaleY="1.8"/>
                    <SkewTransform AngleX="0" AngleY="0" CenterX="0.5" CenterY="0.848"/>
                    <RotateTransform Angle="-33.418" CenterX="0.5" CenterY="0.848"/>
                    <TranslateTransform Y="0.278"/>
                </TransformGroup>
            </RadialGradientBrush.RelativeTransform>
        </RadialGradientBrush>
    </Rectangle.Fill>
</Rectangle>

在资源中,我有几个像这样的 RadialGradientBrush 对象。

<RadialGradientBrush x:Key="EscalatedGlow" Center="0.5,0.848" GradientOrigin="0.5,0.818" RadiusX="-1.424" RadiusY="-0.622">
    <RadialGradientBrush.RelativeTransform>
        <TransformGroup>
            <ScaleTransform CenterX="0.5" CenterY="0.848" ScaleX="1" ScaleY="1.8"/>
            <SkewTransform AngleX="0" AngleY="0" CenterX="0.5" CenterY="0.848"/>
            <RotateTransform Angle="-33.418" CenterX="0.5" CenterY="0.848"/>
            <TranslateTransform Y="0.278"/>
        </TransformGroup>
    </RadialGradientBrush.RelativeTransform>
    <GradientStop Color="Aqua" Offset="0.168"/>
    <GradientStop Color="#5E1D96FF" Offset="0.474"/>
    <GradientStop Color="#1101FFFF" Offset="1"/>
</RadialGradientBrush>

我想用更少的代码来替换每次颜色更改,因此我在资源中创建了一些 GradientStopCollection 对象来替换它们。

<GradientStopCollection x:Key="EscalatedGradient">
    <GradientStop Color="Aqua" Offset="0.168"/>
    <GradientStop Color="#5E1D96FF" Offset="0.474"/>
    <GradientStop Color="#1101FFFF" Offset="1"/>
</GradientStopCollection>

虽然我可以绑定到资源渐变,但问题是我无法访问 GlowGradient 画笔来更改其 GradientStops 属性。我以前可以使用 DataTrigger 和以下内容来访问发光矩形。

<DataTrigger Binding="{Binding Path=Status}" Value="Escalated">
    <Setter TargetName="Glow" Property="Fill" Value="{StaticResource EscalatedGlow}"/>
</DataTrigger>

当我使用以下命令时,出现“找不到触发目标”错误。

<DataTrigger Binding="{Binding Path=Status}" Value="Escalated">
        <Setter TargetName="GlowGradient" Property="GradientStops" Value="{StaticResource EscalatedGradient}"/>
</DataTrigger>

我想必须有一种方法可以让我不必每次想要更改颜色时都复制整个 RadialGraientBrush。有没有办法从 DataTrigger 访问矩形填充画笔?有人有什么建议吗?提前致谢。

I have a DataTemplate with a number of layered text and graphic objects. One of them is a glow effect that comes from the RadialGradientBrush Fill property of a Rectangle. At first, I named the Rectangle and bound to the Fill property and changed it using a DataTrigger. This worked fine, but I have a number of RadialGradientBrush objects in the Resources section and as you can see below, it is a lot to repeat when all I want to do is change the GradientStops. So I removed the Fill binding and added and named a RadialGradientBrush and although I can bind to the brush from Resources, I can't access it in the DataTrigger. I get the 'Cannot find Trigger target' error.

<Rectangle x:Name="Glow" IsHitTestVisible="False" RadiusX="1.5" RadiusY="1.5" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" StrokeThickness="0" Opacity="1.0">
    <Rectangle.Fill>
        <RadialGradientBrush x:Name="GlowGradient" Center="0.5,0.848" GradientOrigin="0.5,0.818" RadiusX="-1.424" RadiusY="-0.622" GradientStops="{StaticResource DefaultGradient}">
            <RadialGradientBrush.RelativeTransform>
                <TransformGroup>
                    <ScaleTransform CenterX="0.5" CenterY="0.848" ScaleX="1" ScaleY="1.8"/>
                    <SkewTransform AngleX="0" AngleY="0" CenterX="0.5" CenterY="0.848"/>
                    <RotateTransform Angle="-33.418" CenterX="0.5" CenterY="0.848"/>
                    <TranslateTransform Y="0.278"/>
                </TransformGroup>
            </RadialGradientBrush.RelativeTransform>
        </RadialGradientBrush>
    </Rectangle.Fill>
</Rectangle>

In the resources, I have several RadialGradientBrush objects like this one.

<RadialGradientBrush x:Key="EscalatedGlow" Center="0.5,0.848" GradientOrigin="0.5,0.818" RadiusX="-1.424" RadiusY="-0.622">
    <RadialGradientBrush.RelativeTransform>
        <TransformGroup>
            <ScaleTransform CenterX="0.5" CenterY="0.848" ScaleX="1" ScaleY="1.8"/>
            <SkewTransform AngleX="0" AngleY="0" CenterX="0.5" CenterY="0.848"/>
            <RotateTransform Angle="-33.418" CenterX="0.5" CenterY="0.848"/>
            <TranslateTransform Y="0.278"/>
        </TransformGroup>
    </RadialGradientBrush.RelativeTransform>
    <GradientStop Color="Aqua" Offset="0.168"/>
    <GradientStop Color="#5E1D96FF" Offset="0.474"/>
    <GradientStop Color="#1101FFFF" Offset="1"/>
</RadialGradientBrush>

I want to replace them with less code for each colour change, so I created some GradientStopCollection objects in the Resources to replace them with.

<GradientStopCollection x:Key="EscalatedGradient">
    <GradientStop Color="Aqua" Offset="0.168"/>
    <GradientStop Color="#5E1D96FF" Offset="0.474"/>
    <GradientStop Color="#1101FFFF" Offset="1"/>
</GradientStopCollection>

Although I can bind to the Resource gradients, the problem is that I can't access the GlowGradient brush to change its GradientStops property. I could previously access the Glow Rectangle using a DataTrigger with the following.

<DataTrigger Binding="{Binding Path=Status}" Value="Escalated">
    <Setter TargetName="Glow" Property="Fill" Value="{StaticResource EscalatedGlow}"/>
</DataTrigger>

When I use the following, I get the 'Cannot find Trigger target' error.

<DataTrigger Binding="{Binding Path=Status}" Value="Escalated">
        <Setter TargetName="GlowGradient" Property="GradientStops" Value="{StaticResource EscalatedGradient}"/>
</DataTrigger>

I'm thinking there just has to be a way to save me from replicating the whole RadialGraientBrush each time I want to change the colours. Is there any way to access the Rectangle Fill brush from the DataTrigger? Any tips anyone? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我鬧 2024-10-07 01:06:47

最后,我使用了以下代码:

<Rectangle Name="Glow" IsHitTestVisible="False" RadiusX="2.5" RadiusY="2.5" 
Fill="{StaticResource OrangeGlow}" />

<Storyboard x:Key="GlowColourStoryboard" TargetName="Glow" Duration="0:0:1.5" 
AutoReverse="True" BeginTime="0:0:0" RepeatBehavior="Forever">
    <ColorAnimation Storyboard.TargetProperty="Fill.GradientStops[0].Color" 
To="{StaticResource RedGradient.Colour1}" />
    <ColorAnimation Storyboard.TargetProperty="Fill.GradientStops[1].Color" 
To="{StaticResource RedGradient.Colour2}" />
    <ColorAnimation Storyboard.TargetProperty="Fill.GradientStops[2].Color" 
To="{StaticResource RedGradient.Colour3}" />
</Storyboard>

我没有显示 Brush 资源,因为......好吧,您可以制作自己的资源。这个 Storyboard 按以下方式使用并按要求工作:

<DataTrigger.EnterActions>
    <BeginStoryboard Storyboard="{StaticResource GlowColourStoryboard}" />
</DataTrigger.EnterActions>

In the end, I went with the following code:

<Rectangle Name="Glow" IsHitTestVisible="False" RadiusX="2.5" RadiusY="2.5" 
Fill="{StaticResource OrangeGlow}" />

<Storyboard x:Key="GlowColourStoryboard" TargetName="Glow" Duration="0:0:1.5" 
AutoReverse="True" BeginTime="0:0:0" RepeatBehavior="Forever">
    <ColorAnimation Storyboard.TargetProperty="Fill.GradientStops[0].Color" 
To="{StaticResource RedGradient.Colour1}" />
    <ColorAnimation Storyboard.TargetProperty="Fill.GradientStops[1].Color" 
To="{StaticResource RedGradient.Colour2}" />
    <ColorAnimation Storyboard.TargetProperty="Fill.GradientStops[2].Color" 
To="{StaticResource RedGradient.Colour3}" />
</Storyboard>

I haven't shown the Brush resources because... well you can make your own. This Storyboard is used in the following way and works as required:

<DataTrigger.EnterActions>
    <BeginStoryboard Storyboard="{StaticResource GlowColourStoryboard}" />
</DataTrigger.EnterActions>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文