在VisualStateManager中交换画笔

时间:2015-02-08 19:54:10

标签: c# wpf visualstatemanager visualstates

我试图为具有玻璃外观的画笔创建自定义样式。我看起来就像我想要的那样,但我不能让Pressed行为起作用。

按下的外观只是正常外观,其中一个刷子反转。我已将两个画笔都设置为资源并尝试ObjectAnimationUsingKeyFrames,但它似乎没有。这个想法与此类似:

<VisualState x:Name="Pressed">
    <Storyboard>
        <!-- swap the brush from {StaticResource ButtonGlassBrushNormal} to 
             {StaticResource ButtonGlassBrushPressed -->
    </Storyboard>
</VisualState>

以下是我的资源和风格:

<LinearGradientBrush x:Key="ButtonGlassBrushNormal" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#00000000" Offset="0.31"/>
    <GradientStop Color="White"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="ButtonGlassBrushPressed" EndPoint="0.5,0" StartPoint="0.5,1">
    <GradientStop Color="White"/>
    <GradientStop Color="#00000000" Offset="0.31"/>
</LinearGradientBrush>
<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal" />
                <VisualState x:Name="Pressed">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames
                            Storyboard.TargetName="_glassBorder"
                            Storyboard.TargetProperty="Background"
                            Duration="0:0:1"
                            RepeatBehavior="Forever">
                            <ObjectAnimationUsingKeyFrames.KeyFrames>
                                <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{StaticResource ButtonGlassBrushPressed}" />
                            </ObjectAnimationUsingKeyFrames.KeyFrames>  
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" VerticalAlignment="Top" Margin="0,0,0,-21.167" CornerRadius="5">
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="Black" Offset="0"/>
                    <GradientStop Color="#FF803A3A" Offset="1"/>
                </LinearGradientBrush>
            </Border.Background>
            <Border BorderThickness="1" CornerRadius="5" x:Name="_glassBorder" Background="{StaticResource ButtonGlassBrushNormal}">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </Border>
    </Grid>
</ControlTemplate>

我猜这会很简单,但我在这里做错了什么?

2 个答案:

答案 0 :(得分:3)

不要在故事板上设置持续时间,也可以设置为零KeyTime。此外,您还必须声明MouseOver状态,否则Pressed状态将持续直到鼠标离开Button:

<VisualStateGroup x:Name="CommonStates">
    <VisualState x:Name="Normal"/>
    <VisualState x:Name="MouseOver"/>
    <VisualState x:Name="Disabled"/>
    <VisualState x:Name="Pressed">
        <Storyboard>
            <ObjectAnimationUsingKeyFrames
                Storyboard.TargetName="_glassBorder"
                Storyboard.TargetProperty="Background">
                <ObjectAnimationUsingKeyFrames.KeyFrames>
                    <DiscreteObjectKeyFrame KeyTime="0:0:0"
                        Value="{StaticResource ButtonGlassBrushPressed}"/>
                </ObjectAnimationUsingKeyFrames.KeyFrames>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </VisualState>
</VisualStateGroup>

答案 1 :(得分:0)

如果你不关心动画(就像我一样),那就是你应该做的事情:

<VisualState x:Name="Pressed">
      <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="_glassBorder">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonGlassBrushPressed}"/>
            </ObjectAnimationUsingKeyFrames>
      </Storyboard>
</VisualState>