跳过正文

WPF change button style with image

Tech WPF .Net
浮世絵空事
作者
浮世絵空事
所谓灵魂 几行代码

WPF change button style with image

Style:

<Style TargetType="Button" x:Key="BtnLoginImg">
    <Setter Property="Width" Value="220"/>
    <Setter Property="Height" Value="45"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="Grid">
                    <Image Name="ImgBtnLogin" Source="../Resources/Img/btn_default.png"/>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      Margin="{TemplateBinding Padding}"
                                      RecognizesAccessKey="True"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="Button.IsMouseOver" Value="true">
                        <Setter Property="Image.Source" TargetName="ImgBtnLogin" Value="../Resources/Img/btn_mouseover.png"/>
                    </Trigger>
                    <Trigger Property="Button.IsPressed" Value="true">
                        <Setter Property="Image.Source" TargetName="ImgBtnLogin" Value="../Resources/Img/btn_press.png"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>