【问题标题】:Using an image as a button in Silverlight. Why is my mouseOver method not working?在 Silverlight 中使用图像作为按钮。为什么我的 mouseOver 方法不起作用?
【发布时间】:2012-08-31 01:57:00
【问题描述】:

我想使用图像在 Silverlight 中创建一个按钮,该按钮具有使按钮看起来可点击的基本效果。我创建了一个 .png 图标并编写了一些代码,以使该图像在情节提要中的鼠标悬停时稍大一些。这是我第一次尝试做这样的事情,所以我假设我遗漏了对其中一个静态资源的一些简单调用。我错过了什么? :

<Style TargetType="Button"
                 x:Key="styleA">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid x:Name="RootElement"
                            Background="Transparent">
                            <Grid.Resources>
                                <Storyboard x:Key="MouseOver State">
                                    <DoubleAnimation Storyboard.TargetName="buttonScale"
                                                Storyboard.TargetProperty="ScaleX"
                                                To="1.2"
                                                Duration="00:00:00.25"/>
                                    <DoubleAnimation Storyboard.TargetName="buttonScale"
                                                Storyboard.TargetProperty="ScaleY"
                                                To="1.2"
                                                Duration="00:00:00.25" />
                                </Storyboard>

                                <Storyboard x:Key="Normal State">
                                    <DoubleAnimation Storyboard.TargetName="buttonScale"
                                                Storyboard.TargetProperty="ScaleX"
                                                To="1"
                                                Duration="00:00:00.25" />
                                    <DoubleAnimation Storyboard.TargetName="buttonScale"
                                                Storyboard.TargetProperty="ScaleY"
                                                To="1"
                                                Duration="00:00:00.25" />
                                </Storyboard>

                                <Storyboard x:Key="Pressed State">
                                    <DoubleAnimation Storyboard.TargetName="buttonScale"
                                                Storyboard.TargetProperty="ScaleX"
                                                To="1.4"
                                                Duration="00:00:00.25" />
                                    <DoubleAnimation Storyboard.TargetName="buttonScale"
                                                Storyboard.TargetProperty="ScaleY"
                                                To="1.4"
                                                Duration="00:00:00.25" />
                                </Storyboard>


                            </Grid.Resources>
                            <ContentPresenter Content="{TemplateBinding Content}"
                                           RenderTransformOrigin="0.5,0.5">
                                <ContentPresenter.RenderTransform>
                                    <ScaleTransform x:Name="buttonScale" />
                                </ContentPresenter.RenderTransform>
                            </ContentPresenter>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>

            </Setter>
        </Style>

如您所见,一个足够简单的故事板。现在,当我实现我的按钮时,什么也没有发生。我究竟做错了什么?这是我创建按钮的代码:

    <StackPanel>
        <Button Width="50"
           x:Name="Test"
           Style="{StaticResource styleA}">
            <StackPanel Orientation="Vertical">
                <Image Source="test.png" />
                <TextBlock Text="Please get bigger" />
            </StackPanel>
        </Button>
    </StackPanel>

【问题讨论】:

    标签: c# silverlight xaml button storyboard


    【解决方案1】:

    你看过Button Styles and Templates吗?您需要将这些故事板放在VisualStateManager.VisualStateGroups 中,如下所示:

    <ControlTemplate TargetType="Button">
        <Grid x:Name="RootElement" Background="Transparent">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="MouseOver">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="buttonScale" Storyboard.TargetProperty="ScaleX" To="1.2" Duration="00:00:00.25"/>
                            <DoubleAnimation Storyboard.TargetName="buttonScale" Storyboard.TargetProperty="ScaleY" To="1.2" Duration="00:00:00.25" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Pressed">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="buttonScale" Storyboard.TargetProperty="ScaleX" To="1.4" Duration="00:00:00.25" />
                            <DoubleAnimation Storyboard.TargetName="buttonScale" Storyboard.TargetProperty="ScaleY" To="1.4" Duration="00:00:00.25" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Normal"/>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <ContentPresenter Content="{TemplateBinding Content}" RenderTransformOrigin="0.5,0.5">
                <ContentPresenter.RenderTransform>
                    <ScaleTransform x:Name="buttonScale" />
                </ContentPresenter.RenderTransform>
            </ContentPresenter>
        </Grid>
    </ControlTemplate>
    

    【讨论】:

    • 我已经添加了这个,不幸的是我的按钮仍然没有变大:(。我需要在我的按钮中拨打一些电话吗?
    • @SimonKiely,你不需要在按钮中做任何事情,当相应的事件发生时会自动触发一个状态。究竟是什么行不通?你有什么错误吗?看我的编辑,也许你离开了Grid.Resources中的状态。
    猜你喜欢
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2019-08-03
    • 2018-02-27
    • 2015-08-13
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    相关资源
    最近更新 更多