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.
我有一個DataTemplate,包含許多分層文本和圖形對象。其中一個是來自Rectangle的RadialGradientBrush Fill屬性的發光效果。首先,我命名了Rectangle並綁定到Fill屬性並使用DataTrigger更改它。這很好用,但是我在參考資料部分有很多RadialGradientBrush對象,正如你在下面看到的那樣,當我想要做的就是改變GradientStops時,要重復這個對象。所以我刪除了Fill綁定並添加並命名了RadialGradientBrush,雖然我可以從Resources綁定到畫筆,但我無法在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>
In the resources, I have several RadialGradientBrush objects like this one.
在資源中,我有幾個像這樣的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>
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對象來替換它們。
<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.
雖然我可以綁定到Resource漸變,但問題是我無法訪問GlowGradient畫筆來更改其GradientStops屬性。我之前可以使用帶有以下內容的DataTrigger訪問Glow Rectangle。
<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.
我想當每次我想要改變顏色時,必須有辦法讓我不要復制整個RadialGraientBrush。有沒有辦法從DataTrigger訪問矩形填充畫筆?有人提示嗎?提前致謝。
1
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:
我沒有顯示刷資源,因為......你可以自己制作。此Storyboard按以下方式使用,並按要求工作:
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource GlowColourStoryboard}" />
</DataTrigger.EnterActions>
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/10/29/725c37e72d1f9b69d3677541fc38b9eb.html。