c# - 在悬停时更改按钮的图像背景并单击

时间:2014-04-08 09:37:13

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

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

怎么做?请帮帮我

即时通讯使用visual studio 2013 - windows store - 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>

我在这里改变了什么吗?

2 个答案:

答案 0 :(得分:1)

您所描述的行为是Template的默认Button的一部分。要自定义此行为,您可以定义自定义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

答案 1 :(得分:0)

试试这个

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