更改按钮图像悬停或点击 [英] Change Button Image On Hover or Click

查看:158
本文介绍了更改按钮图像悬停或点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何改变一个按钮的悬停和点击的背景图像?在Visual Studio的用户界面似乎并没有提供这样做的任何简单的方法。目前默认的行为似乎与纯色,它看起来aweful来取代我的形象。

所有我至今是按钮基部:

 <按钮内容=HEIGHT =75VerticalAlignment =中心WIDTH =75的Horizo​​ntalAlignment =中心ClickMode =preSS>
        < Button.Background>
            <的ImageBrush的ImageSource =../数据/图像/图标/ skill_icon_0.png/>
        < /Button.Background>
    < /按钮>

我试图处理事件并手动设置,但它不会为pressed /发行工作:

 按钮skillButton =新按钮();
        skillButton.Width = 75;
        skillButton.Height = 75;
        skillButton.ClickMode = ClickMode preSS。
        skillButton.Background = GetIconImage(iconIndex,0);
        skillButton.PointerEntered + =
            (对象发件人,Windows.UI.Xaml.Input.PointerEventArgs E)=> {
                skillButton.Background = GetIconImage(iconIndex,1);
            };
        skillButton.PointerExited + =
            (对象发件人,Windows.UI.Xaml.Input.PointerEventArgs E)=> {
                skillButton.Background = GetIconImage(iconIndex,0);
            };
        skillButton.Pointer pressed + =
            (对象发件人,Windows.UI.Xaml.Input.PointerEventArgs E)=> {
                skillButton.Background = GetIconImage(iconIndex,2);
            };
        skillButton.PointerReleased + =
            (对象发件人,Windows.UI.Xaml.Input.PointerEventArgs E)=> {
                如果(skillButton.FocusState == FocusState.Pointer)
                    skillButton.Background = GetIconImage(iconIndex,1);
                别的skillButton.Background = GetIconImage(iconIndex,0);
            };


解决方案

我结束了编辑控件模板只是创建和更改边框。但它可被用来改变图像太

 <按钮宽度=75HEIGHT =75ClickMode =preSS>
        < Button.Template>
            <的ControlTemplate的TargetType =按钮>
                < BORDER X:NAME =rootElement的CornerRadius =10了borderThickness =2>
                    < VisualStateManager.VisualStateGroups>
                        < VisualStateGroup X:NAME =CommonStates>
                            < VisualStateGroup.Transitions>
                                &所述; VisualTransition若要=鼠标悬停GeneratedDuration =0:0:0.1/>
                                &所述; VisualTransition若要=pressedGeneratedDuration =0:0:0.1/>
                            < /VisualStateGroup.Transitions>
                            <的VisualState X:名称=正常/>
                            <的VisualState X:NAME =鼠标悬停>
                                <情节提要>
                                    < ColorAnimation Storyboard.TargetName =BorderBrush
                                                    Storyboard.TargetProperty =颜色
                                                    为了=黄色/>
                                < /故事板>
                            < /&的VisualState GT;
                            <的VisualState X:NAME =pressed>
                                <情节提要>
                                    < ColorAnimation Storyboard.TargetName =BorderBrush
                                                    Storyboard.TargetProperty =颜色
                                                    为了=黑/>
                                < /故事板>
                            < /&的VisualState GT;
                        < / VisualStateGroup>
                    < /VisualStateManager.VisualStateGroups>
                    < Border.BorderBrush>
                        <的SolidColorBrush X:NAME =BorderBrush颜色=白/>
                    < /Border.BorderBrush>
                    < Border.Background>
                        <的ImageBrush的ImageSource =MS-APPX:/data/images/icons/skill_icon_0.png/>
                    < /Border.Background>
                < /边框>
            < /控件模板>
        < /Button.Template>
    < /按钮>

How do I change the background image of a Button on Hover and on Click? The Visual Studio's UI does not seem to provide any easy way of doing it. Currently the default behavior seems to replace my image with a solid color, which looks aweful.

All I have so far is the Button base:

    <Button Content="" Height="75" VerticalAlignment="Center" Width="75" HorizontalAlignment="Center" ClickMode="Press">
        <Button.Background>
            <ImageBrush ImageSource="../data/images/icons/skill_icon_0.png"/>
        </Button.Background>
    </Button>

I tried to handle events and manually set it, but it doesn't work for Pressed/Released:

        Button skillButton = new Button();
        skillButton.Width = 75;
        skillButton.Height = 75;
        skillButton.ClickMode = ClickMode.Press;
        skillButton.Background = GetIconImage(iconIndex, 0);
        skillButton.PointerEntered += 
            (object sender, Windows.UI.Xaml.Input.PointerEventArgs e) => {
                skillButton.Background = GetIconImage(iconIndex, 1);
            };
        skillButton.PointerExited +=
            (object sender, Windows.UI.Xaml.Input.PointerEventArgs e) => {
                skillButton.Background = GetIconImage(iconIndex, 0);
            };
        skillButton.PointerPressed +=
            (object sender, Windows.UI.Xaml.Input.PointerEventArgs e) => {
                skillButton.Background = GetIconImage(iconIndex, 2);
            };
        skillButton.PointerReleased +=
            (object sender, Windows.UI.Xaml.Input.PointerEventArgs e) => {
                if (skillButton.FocusState == FocusState.Pointer)
                    skillButton.Background = GetIconImage(iconIndex, 1);
                else skillButton.Background = GetIconImage(iconIndex, 0);
            };

解决方案

I ended up editing the ControlTemplate just to create and change the Border. But it could be used to change the image too.

    <Button Width="75" Height="75" ClickMode="Press">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Border x:Name="RootElement" CornerRadius="10" BorderThickness="2">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition To="MouseOver" GeneratedDuration="0:0:0.1"/>
                                <VisualTransition To="Pressed" GeneratedDuration="0:0:0.1"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BorderBrush" 
                                                    Storyboard.TargetProperty="Color" 
                                                    To="Yellow" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="BorderBrush" 
                                                    Storyboard.TargetProperty="Color"
                                                    To="Black"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border.BorderBrush>
                        <SolidColorBrush x:Name="BorderBrush" Color="White"/>
                    </Border.BorderBrush>
                    <Border.Background>
                        <ImageBrush ImageSource="ms-appx:/data/images/icons/skill_icon_0.png"/>
                    </Border.Background>
                </Border>
            </ControlTemplate>
        </Button.Template>
    </Button>

这篇关于更改按钮图像悬停或点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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