我怎样才能改变一个按钮的背景图像时悬停或单击XAML为Windows 8? [英] How can i change background image of a button when hover or click in XAML for Windows 8?

查看:110
本文介绍了我怎样才能改变一个按钮的背景图像时悬停或单击XAML为Windows 8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题关于它的点击时更改按钮的背景图片。

I got a question about changing the background image of a button when it's clicked.

我试着在谷歌搜索,但我无法找到的东西,为我工作。

I tried searching on google but i couldn't find something that worked for me.

那么这里是我的code

Well here's my code

<Button Grid.Column="0" Grid.Row="0" x:Name="btnHome" VerticalAlignment="Stretch" BorderBrush="{x:Null}" HorizontalAlignment="Stretch" Click="btnHome_Click" PointerEntered="btnHome_PointerEntered">
                <Button.Template>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="RootElement">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            //I want my background change here
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            //And here
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border.Background>
                                <ImageBrush ImageSource="images/back_button.png"/>
                            </Border.Background>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>

我也试过这种

private void btnHome_PointerEntered(object sender, PointerRoutedEventArgs e)
    {
        BitmapImage bmp = new BitmapImage();
        Uri u = new Uri("ms-appx:images/back_button_mouseover.png", UriKind.RelativeOrAbsolute);
        bmp.UriSource = u;
        ImageBrush i = new ImageBrush();
        i.ImageSource = bmp;
        btnHome.Background = i;
    }

不过遗憾的是这个工作既不

But unfortunately this worked neither

推荐答案

我发现在这个问题上anwser。

I found the anwser on this question.

您必须编辑控件的模板。

You have to edit the template of your control.

您可以用混合做到这一点,或手动。

You can do it with Blend, or "by hand".

找回按钮的默认模板(由pressing右键,编辑模板,编辑复制,给它一个名称和preSS确定),然后对其进行自定义,以满足您的需求。

Retrieve the default template of your button(by pressing right button, edit template, edit copy, give it a name and press ok) and then customize it in order to fit to your needs

编辑:这里是MSDN的ressource在单选模板定制。

EDIT : Here is MSDN Ressource on RadioButton template customization.

这篇关于我怎样才能改变一个按钮的背景图像时悬停或单击XAML为Windows 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆