WPF VisualStateManager.GoToState

时间:2013-05-12 10:51:20

标签: wpf visualstatemanager

我尝试更改程序中所有按钮的状态

我有按钮的这种风格:

 <Style TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="StateMouseOver">
                                    <Storyboard>
                                        <ColorAnimation  Storyboard.TargetName="UpperBorder" Duration="00:00:0.5"  Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="#FF5086C4"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="StatePressed">
                                    <Storyboard>
                                        <ColorAnimation  Storyboard.TargetName="UpperBorder" Duration="00:00:0.05"  Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="#FF21214E"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="StateDisabled">
                                    <Storyboard>
                                        <DoubleAnimation  Storyboard.TargetName="UpperBorder" Duration="00:00:0.5"  Storyboard.TargetProperty="(UIElement.Opacity)" To="0"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Lower" Cursor="Hand"   BorderThickness="1" CornerRadius="4" Background="Gray" ></Border>
                        <Border MouseEnter="UpperBorder_MouseEnter" x:Name="UpperBorder" Cursor="Hand"  CornerRadius="4"   Background="#FF0657E8">
                            <Border.Effect>
                                <DropShadowEffect BlurRadius="10"/>
                            </Border.Effect>
                        </Border>
                        <ContentPresenter Cursor="Hand" Margin="8,8,8,8"  HorizontalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

在此事件中:MouseEnter="UpperBorder_MouseEnter我希望更改将在我的程序中 所有按钮 生效(但VisualStateManager.GoToState函数作为参数只有特定按钮)
我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

button已在CommonStates组中具有MouseOver状态。见Button Styles and Templates。鼠标悬停时,Button类中的状态自动设置。

这就是你想要的吗?