触发器中的WPF ControlTemplate样式GradientStop [英] WPF ControlTemplate Style GradientStop in Trigger

查看:108
本文介绍了触发器中的WPF ControlTemplate样式GradientStop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的TabItem的XAML.我希望能够在触发器中设置单个渐变停止的颜色.我知道我可以在触发器的设置器中完全重新定义渐变,但是我想在后台访问特定属性,以便将来对其进行动画处理.

Here is my XAML for a TabItem. I want to be able to set the Color of a single gradient stop in a trigger. I know that I can re-define the gradient completely in the trigger's setter, but I want to access a specific property on the background so I can animate it in the future.

我已经尝试了触发器设置程序中所有内容的所有变体,并用谷歌搜索了很长时间-但我仍然无法对其进行编译.我也尝试过class.property语法,但是什么也没有.该代码引发的当前错误是:

I have tried every variation of everything in the trigger's setter and googled for a long time - but I still can't get it to compile. I have also tried class.property syntax, but still nothing. The current error this code raises is:

未找到类型'Background.GradientStops [0]'."

"Type 'Background.GradientStops[0]' was not found."

我很确定我知道这里发生了什么,也许我想要的是不可能的.但是必须有一种方法可以在控件模板中为控件的渐变设置动画...

I am pretty sure I know what is going on here - and perhaps what I want is impossible. But there has to be a way to animate a control's gradient in a control template...

有人可以帮助我吗? 谢谢

Can anyone help me? thanks

<Style TargetType="{x:Type TabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <TextBlock Padding="6 2 6 2" Name="TheHeader">
                    <TextBlock.Background>
                        <LinearGradientBrush StartPoint="0, 0" EndPoint="0, 1">
                            <GradientStop Offset="0" Color="#f4fafd" />
                            <GradientStop Offset="1" Color="#ceedfa" />
                        </LinearGradientBrush>
                    </TextBlock.Background>
                    <ContentPresenter ContentSource="Header" Margin="0" />
                </TextBlock>
                <ControlTemplate.Triggers >
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="TheHeader" Property="Background.GradientStops[0].Color" Value="White" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

推荐答案

您可以对其进行动画处理,例如示例此处.

You can animate it, like in the example here.

您也可以使用一些技巧来进行设置,尽管我总是更喜欢将多个画笔作为资源并交换它们,或者像您提到的那样在其中重新创建画笔.

You also could use a slight hack to set it, though I always prefer creating multiple brushes as resources and swapping them or re-creating a brush in the as you mentioned.

<Style TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <TextBlock Padding="6 2 6 2"
                               Name="TheHeader" Tag="#f4fafd">
                                    <TextBlock.Background>
                                            <LinearGradientBrush StartPoint="0, 0"
                                                                 EndPoint="0, 1">
                                                    <GradientStop Offset="0" 
                                                                  Color="{Binding ElementName=TheHeader, Path=Tag}"/>
                                                    <GradientStop Offset="1"
                                                                  Color="#ceedfa" />
                                            </LinearGradientBrush>
                                    </TextBlock.Background>
                                    <ContentPresenter ContentSource="Header"
                                                      Margin="0" />
                    </TextBlock>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected"
                                 Value="True">
                            <Setter TargetName="TheHeader"
                                    Property="Tag"
                                    Value="Red" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这篇关于触发器中的WPF ControlTemplate样式GradientStop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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