在WPF中使用图像按钮设置按钮样式

时间:2017-05-10 10:37:10

标签: wpf image xaml button wpf-controls

我需要按照以下方式在WPF中设置按钮样式 enter image description here

文本区域(客户登录)和图标区域(确切地说是整个灰色区域)应该用作按钮
目前,我正在使用两个按钮,一个用于文本部分,另一个用于图标部分,并使用相同的点击事件绑定它们,但我没有得到所需的结果
Form.xaml

<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Button Tag="ByCard" x:Name="btn_CustomerLoginIcon" Grid.Column="0" Style="{StaticResource style_Button_GreyDefault}" MinHeight="90" Width="80" Margin="0,20" FontSize="35" Click="btn_CustomerLogin_Click">
                <StackPanel>
                    <TextBlock>My text here</TextBlock>
                    <Image Source="/Resources/img_businessman.png" Stretch="None" />
                </StackPanel>
            </Button>
            <Button Tag="ByCard" x:Name="btn_CustomerLogin" Grid.Column="0" Style="{StaticResource style_Button_GreyDefault}" MinHeight="90" MinWidth="400" Margin="20" FontSize="35" Content="Customer Login" Click="btn_CustomerLogin_Click" />
            <Button Tag="Manual" x:Name="btn_ConsultantLogin" Grid.Column="1" Style="{StaticResource style_Button_GreyDefault}" MinHeight="90" MinWidth="400" Margin="20" FontSize="35" Content="Consultant Login" Click="btn_ConsultantLogin_Click" />
            <Button Tag="Manual" x:Name="btn_NewUser" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Style="{StaticResource style_Button_RedDefault}" MinHeight="90" MinWidth="400" Margin="20" FontSize="35" Content="New User" Width="80" Click="btn_NewUser_Click" />
        </Grid>
    </StackPanel>

Resource.xaml

<Style x:Key="style_Button_GreyDefault" TargetType="{x:Type Button}">
        <Setter Property="Focusable" Value="False" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Background" Value="{StaticResource color_Greyish}"/>
        <Setter Property="BorderBrush" Value="{StaticResource color_White}"/>
        <Setter Property="BorderThickness" Value="5"/>
        <Setter Property="Foreground" Value="{StaticResource color_BlueDefault}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="16,3,16,3"/>
        <!--<Setter Property="FontFamily" Value="Avenir-Medium"/>-->
        <Setter Property="FontFamily" Value="Arial Bold"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="FontSize" Value="26"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="Chrome"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="10"
                                SnapsToDevicePixels="true">
                        <ContentPresenter Name="Presenter" Margin="{TemplateBinding Padding}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    RecognizesAccessKey="True"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource color_Gray}" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{StaticResource color_BlueDefault}" />
                            <Setter Property="BorderBrush" Value="{StaticResource color_BlueDefault}" />
                            <Setter Property="Foreground" Value="{StaticResource color_White}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{StaticResource color_BlueDefault}" />
                            <Setter Property="BorderBrush" Value="{StaticResource color_White}"/>
                            <Setter Property="Foreground" Value="{StaticResource color_White}"/>
                            <Setter Property="Button.Effect">
                                <Setter.Value>
                                    <DropShadowEffect Color="Black" Direction="320" ShadowDepth="3" BlurRadius="5" Opacity="0.5" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <!--<Trigger Property="IsFocused" Value="true">
                            <Setter TargetName="Chrome" Property="BorderBrush" Value="{StaticResource color_White}" />
                        </Trigger>-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="style_Button_GreyIcon" TargetType="{x:Type Button}">
        <Setter Property="Focusable" Value="False" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Background" Value="{StaticResource color_Greyish}"/>
        <Setter Property="BorderBrush" Value="{StaticResource color_White}"/>
        <Setter Property="BorderThickness" Value="5"/>
        <Setter Property="Foreground" Value="{StaticResource color_BlueDefault}"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="16,3,16,3"/>
        <!--<Setter Property="FontFamily" Value="Avenir-Medium"/>-->
        <Setter Property="FontFamily" Value="Arial Bold"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="FontSize" Value="26"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="Chrome"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="50"
                                SnapsToDevicePixels="true">
                        <ContentPresenter Name="Presenter" Margin="{TemplateBinding Padding}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    RecognizesAccessKey="True"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource color_Gray}" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{StaticResource color_BlueDefault}" />
                            <Setter Property="BorderBrush" Value="{StaticResource color_BlueDefault}" />
                            <Setter Property="Foreground" Value="{StaticResource color_White}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{StaticResource color_BlueDefault}" />
                            <Setter Property="BorderBrush" Value="{StaticResource color_White}"/>
                            <Setter Property="Foreground" Value="{StaticResource color_White}"/>
                            <Setter Property="Button.Effect">
                                <Setter.Value>
                                    <DropShadowEffect Color="Black" Direction="320" ShadowDepth="3" BlurRadius="5" Opacity="0.5" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <!--<Trigger Property="IsFocused" Value="true">
                            <Setter TargetName="Chrome" Property="BorderBrush" Value="{StaticResource color_White}" />
                        </Trigger>-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这就是我现在要做的事情 The button I'm getting
我的方法是否正确?或者有更好的方法来实现这一目标吗?我假设我需要做以下事项

  • 我需要以这样的方式增加“图像”按钮的高度 不会影响文字按钮的高度
  • 我需要在两个按钮之间应用适当的填充

1 个答案:

答案 0 :(得分:1)

这是一些粗略的草图,看看是否可以将你设置在正确的轨道上。 该代码产生了这个:

enter image description here

我没有打扰让它看起来像你的,只是像你问的那样对它进行改变。

 <ControlTemplate x:Key="ButtonControlTemplate1"
                         TargetType="{x:Type Button}">
            <Grid Width="180">
                <Grid.RowDefinitions>
                    <RowDefinition Height="10" />
                    <RowDefinition Height="40" />
                    <RowDefinition Height="10" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Border x:Name="LabelSection"
                        Grid.Column="1"
                        Grid.Row="1"
                        Margin="-20 0 0 0"
                        Background="#FFD4D4D4"
                        BorderThickness="2"
                        BorderBrush="White"
                        CornerRadius="5">
                    <Label Content="{Binding Content,RelativeSource={RelativeSource AncestorType=Button}}"
                           Foreground="Blue"
                           Margin="20 0 0 0" 
                           VerticalAlignment="Center"/>
                </Border>
                <Border x:Name="ImageSection"
                        Grid.Row="0"
                        Grid.RowSpan="3"
                        Background="#FFD4D4D4"
                        BorderThickness="2"
                        BorderBrush="White"
                        CornerRadius="25"
                        Width="50"
                        Height="50">
                    <Image Source="man15.png"
                           Height="35" />
                </Border>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver"
                         Value="true">
                    <Setter TargetName="ImageSection"
                            Property="Background"
                            Value="lightGreen" />
                    <Setter TargetName="LabelSection"
                            Property="Background"
                            Value="lightGreen" />
                </Trigger>
                <Trigger Property="IsPressed"
                         Value="true">
                    <Setter TargetName="ImageSection"
                            Property="Background"
                            Value="Yellow" />
                    <Setter TargetName="LabelSection"
                            Property="Background"
                            Value="Yellow" />
                </Trigger>

            </ControlTemplate.Triggers>
        </ControlTemplate>

我在模板中更新标签绑定

 <Label Content="{Binding Content,RelativeSource={RelativeSource AncestorType=Button}}"

这样,使用此模板的按钮负责标签内容,并将其绑定到该按钮内容。例如,你可以写入按钮Content =&#34; ok&#34;输出将是正常的,或者与第2个按钮一样动态。

<Button Content="Button1"
        Template="{StaticResource ButtonControlTemplate1}"/>

<Button Content="{Binding SomePropertyForContent}"
            Template="{StaticResource ButtonControlTemplate1}"/>