Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在VisualStateManager中交换笔刷_C#_Wpf_Visualstatemanager_Visualstates - Fatal编程技术网

C# 在VisualStateManager中交换笔刷

C# 在VisualStateManager中交换笔刷,c#,wpf,visualstatemanager,visualstates,C#,Wpf,Visualstatemanager,Visualstates,我正在尝试创建一个自定义样式的画笔与玻璃类型的外观。我让它看起来像我想要的那样,但我无法让这种紧迫的行为发挥作用 按下的外观只是正常外观,其中一个画笔反转。我已经将这两个笔刷都设置为资源,并尝试使用关键帧设置对象动画,但似乎不需要。这个想法大致是这样的: <VisualState x:Name="Pressed"> <Storyboard> <!-- swap the brush from {StaticResource ButtonGlas

我正在尝试创建一个自定义样式的画笔与玻璃类型的外观。我让它看起来像我想要的那样,但我无法让这种紧迫的行为发挥作用

按下的外观只是正常外观,其中一个画笔反转。我已经将这两个笔刷都设置为资源,并尝试使用关键帧设置对象动画,但似乎不需要。这个想法大致是这样的:

<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>


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

不要在故事板上设置持续时间,或者设置零键时间。此外,您还必须声明鼠标悬停状态,否则按下的状态将持续到鼠标离开按钮:

<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>

如果你不关心动画(像我一样),你应该这样做:


这是持续时间和零键时间造成的。谢谢
<VisualState x:Name="Pressed">
      <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="_glassBorder">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonGlassBrushPressed}"/>
            </ObjectAnimationUsingKeyFrames>
      </Storyboard>
</VisualState>