【问题标题】:c# - changing image background of button on hover and clickc# - 在悬停并单击时更改按钮的图像背景
【发布时间】:2014-04-08 09:37:12
【问题描述】:

我添加了一个按钮,然后将其背景更改为图片。当它运行时,如果我将鼠标悬停在按钮上,图片就消失了,当我单击时,它会变成纯白色 我想改变它..我想在悬停时显示图片并点击

如何做到这一点?请帮帮我

我正在使用 Visual Studio 2013 - Windows 商店 - c#/xaml

这是我的 btton 的 xaml 代码

 <Button Content="Asia" HorizontalAlignment="Left" Margin="232,366,0,0" VerticalAlignment="Top" Height="395" Width="235" BorderThickness="0">
        <Button.Background>
            <ImageBrush ImageSource="south amirica.png"/>
        </Button.Background>
    </Button>

我要在这里改变什么吗??

【问题讨论】:

    标签: c# xaml windows-8 windows-store-apps


    【解决方案1】:

    您所描述的行为是Button 的默认Template 的一部分。要自定义此行为,您可以定义您的自定义 Button.Template

    <Button Content="Asia" HorizontalAlignment="Left" Margin="232,366,0,0" VerticalAlignment="Top" Height="395" Width="235">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Grid.Background>
                        <ImageBrush ImageSource="south amirica.png"/>
                    </Grid.Background>
                    <ContentPresenter/>
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
    

    请注意,这也会删除所有其他效果,例如按钮按下效果,但您可以根据需要将其添加到您的 Template

    【讨论】:

    • 您能告诉我如何将按钮单击添加到我的模板中吗?
    • 你能帮帮我吗??
    • 我才发现我不需要点击效果!!非常感谢您提供简单有用的解决方案!!!
    • 没问题@LamaTatwany。很高兴它有帮助。至于按钮按下效果我没有包括它,因为它取决于你想要什么效果。您可以为颜色设置动画、翻译按钮、更改边框粗细等。
    【解决方案2】:

    试试这个

    <Button Height="35" Width="200">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Grid.Resources>
                        <BitmapImage x:Key="NormalButton" UriSource="Assets/NormalButton.png"></BitmapImage>
                        <BitmapImage x:Key="OnMouseOver" UriSource="Assets/OnMouseOver.png"></BitmapImage>
                        <BitmapImage x:Key="OnPresed" UriSource="Assets/OnPresed.png"></BitmapImage>
                    </Grid.Resources>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ImageSource" Storyboard.TargetName="Border">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource OnMouseOver}"/>
                                    </ObjectAnimationUsingKeyFrames>                     
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="ImageSource" Storyboard.TargetName="Border">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource OnPresed}"/>
                                    </ObjectAnimationUsingKeyFrames>                                   
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid>
                        <Grid.Background>
                            <ImageBrush x:Name="Border" ImageSource="{StaticResource NormalButton}"></ImageBrush>
                        </Grid.Background>                      
                        <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Button.Template>        
    </Button>
    

    【讨论】:

    • 我对我的应用程序进行了编辑,它会运行良好,但如果我点击它就会中断:(为什么?
    • 上面的例子在这里工作正常。对不起,我不知道你的情况是什么问题。错误信息是什么?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 2017-03-07
    • 2013-04-25
    • 1970-01-01
    相关资源
    最近更新 更多