更多TAG属性

时间:2015-07-26 22:08:53

标签: c# wpf triggers

我在触发器中将Color分配给GradientStop时遇到了问题(因为触发器无法以GradientStop元素为目标)。所以我读了一些关于这个的文章,并找到了使用Tag的解决方案。这是我的代码:

<Border BorderThickness="1" CornerRadius="1.5" x:Name="border">
    <Border.Tag>
        <Color>#FF28AAE6</Color>
    </Border.Tag>

    <Border.Background>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="{Binding ElementName=border, Path=Tag}" Offset="0" />
            <GradientStop Color="#FF0A78AA" Offset="1" />
        </LinearGradientBrush>
    </Border.Background>
</Border>

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter TargetName="border" Property="Tag" Value="#FF46C8FF" />
    </Trigger>
</ControlTemplate.Triggers>

但我需要在该触发器中设置更多值 - 第二个GradientStop的颜色。我该怎么办?

1 个答案:

答案 0 :(得分:0)

为什么不将背景设置为您想要的新画笔而不是尝试更新画笔属性?

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter TargetName="border" Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Color="#FF46C8FF" Offset="0" />
                    <GradientStop Color="#FF0A78AA" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Trigger>
</ControlTemplate.Triggers>

如果您确实需要按照自己的方式进行操作,可以使用Visibilty="Collapsed"制作一些画布并使用ElementBinding来获取那些

的颜色
<ControlTemplate>
    <Border BorderThickness="1" CornerRadius="1.5" x:Name="border">
        <Border.Background>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                <GradientStop Color="{Binding ElementName=Canvas1, Path=Tag}" Offset="0" />
                <GradientStop Color="{Binding ElementName=Canvas2, Path=Tag}" Offset="1" />
            </LinearGradientBrush>
        </Border.Background>
        <Grid>
            <Canvas Name="Canvas1" Visibility="Collapsed">
                <Canvas.Tag>
                    <Color>#FF28AAE6</Color>
                </Canvas.Tag>
            </Canvas>
            <Canvas Name="Canvas2" Visibility="Collapsed">
                <Canvas.Tag>
                    <Color>#FF0A78AA</Color>
                </Canvas.Tag>
            </Canvas>
        </Grid>
    </Border>

    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="Canvas1" Property="Tag" Value="Yellow" />
            <Setter TargetName="Canvas2" Property="Tag" Value="Green" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>