WPF 按钮背景图片切换

.Net技术 码拜 4年前 (2016-02-26) 702次浏览
本人做的是一个播放器,然后放置了一个按钮按钮的作用是静音。想要点击按钮实现静音的同时,背景图片切换成静音的图片。找了很多方法还是没有实现。高手们帮帮忙啊!
WPF 按钮背景图片切换
解决方案

20

<Style  TargetType="{x:Type Button}">
            <Setter Property ="GroupName" Value="SelectTicket"/>
            <Setter Property="Height" Value="40"></Setter>
            <Setter Property="Width" Value="60"></Setter>
            <Setter Property="FontSize" Value="18"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="ContentContainer"  Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" SnapsToDevicePixels="True" BorderBrush="Transparent"  
                            BorderThickness="0" VerticalAlignment="Center" HorizontalAlignment="Stretch" CornerRadius="2">
                            <TextBlock Text="{TemplateBinding Content}" Foreground="White"   VerticalAlignment="Center"  TextAlignment="Center" HorizontalAlignment="Center"/>
                            <Border.Background>
                                <ImageBrush ImageSource="/Images/Background.png"/>
                            </Border.Background>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger  Property="IsMouseOver"  Value="True">
                                <Setter Property="Background" TargetName="ContentContainer">
                                    <Setter.Value>
                                        <ImageBrush ImageSource="/Images/PressBg.png"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Background.png 与 PressBg.png  都是作为 内容  嵌入到 Image文件夹下

10

 <Setter Property =”GroupName” Value=”SelectTicket”/>
把这个去掉。

10

1:设置 style的Key值       也就是 x:Key=””  具体一个名字,然后在Button 的xaml 上使用staticresource引用。具体去搜索用法
2:Property  设置成其它事件,例如IsFocused 等, 了解 WPF 触发器

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明WPF 按钮背景图片切换
喜欢 (0)
[1034331897@qq.com]
分享 (0)