【问题标题】:Change the background image of a Button when pressed using VisualStateManager使用 VisualStateManager 按下时更改按钮的背景图像
【发布时间】:2011-01-17 10:07:26
【问题描述】:

我有这个按钮:

<Button x:Name="PrevAdIcon" Tag="-1" Visibility="Collapsed" Width="80" Height="80" Click="PrevAd">
    <Button.Background>
        <ImageBrush AlignmentY="Top" Stretch="None" ImageSource="/Images/prev.png"></ImageBrush>
    </Button.Background>
</Button>

当用户按下按钮时,如何将背景更改为/Images/prev-selected.png?它会给他一个反馈,因为它是一个 WP7 应用程序

到目前为止我所拥有的(不工作):

<vsm:VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames BeginTime="00:00:00"  Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <ImageBrush ImageSource="/Images/prev-selected.png" Stretch="Fill"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </vsm:VisualState>

【问题讨论】:

  • 我没有在您的代码中看到任何对您希望切换到的背景的引用 (/Images/prev-selected.png)。
  • @Falanwe 我编辑了。 ImageBrush ImageSource 是 Images/prev-selected.png

标签: silverlight xaml windows-phone-7


【解决方案1】:

据我所知,您无法使用 VisualStateManager 更改 Image 元素上 Source 属性的值。但是,您可以在 ControlTemplate 中添加两个 Image 元素:一个用于 Normal 状态,一个用于 Pressed 状态,并在 Pressed 状态下切换 Visibility。


<VisualState x:Name="Pressed">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="Img">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="PressedImg">
            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

【讨论】:

    【解决方案2】:

    有关如何执行此操作的示例和说明,请参阅 Peter Torr 在“Why can't I change the Background of my Button on a Click event?”上的帖子。

    【讨论】:

      【解决方案3】:

      要向 Derek 的答案添加更多信息,您应该查看 Gambit 的答案 here 以了解完整的 XAML 是否有效

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-07
        • 2016-07-12
        • 2014-10-06
        • 1970-01-01
        • 2017-02-06
        • 1970-01-01
        相关资源
        最近更新 更多