简体   繁体   English

WPF按钮样式和模板

[英]WPF button style and template

So i have the button and its style: 所以我有按钮及其样式:

<Style TargetType="Button" x:Key="BaseButtonStyle">
    <Setter Property="FontWeight" Value="Bold"  />
    <Setter Property="Width" Value="120" />
    <Setter Property="Height" Value="30" />
    <Setter Property="BorderBrush" Value="#1FA3FF"  />
</Style>

<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle"  BasedOn="{StaticResource BaseButtonStyle}">          
    <Setter Property="Background" Value="#1FA3EB"  />
    <Setter Property="Foreground" Value="#FFFFFF"  />
</Style>

<Button Style="{StaticResource MyButtonStyle}" Content="My text" />

It works fine and i have the following results (aquamarine background isn't part of Button, it belongs to Window): 它工作正常,我得到以下结果(蓝绿色背景不是Button的一部分,它属于Window):

在此处输入图片说明

But then i want to replace button's content and change my style so: 但是,然后我想替换按钮的内容并更改我的样式,因此:

<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle"  BasedOn="{StaticResource BaseButtonStyle}">          
    <Setter Property="Background" Value="#1FA3EB"  />
    <Setter Property="Foreground" Value="#FFFFFF"  />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="√" />
                    <TextBlock Text="{TemplateBinding Button.Content}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

and now my button becomes awful: 现在我的按钮变得糟糕了:

在此处输入图片说明

As you can see my button lost its borders and background and it doesn't change its view when mouse is over it. 如您所见,我的按钮丢失了边框和背景,并且当鼠标悬停在其上方时,它的视图也没有改变。 Where am i wrong and what should i do to prevent it? 我在哪里错了,我应该怎么做才能防止它?

UPD UPD

when i do something like this: 当我做这样的事情:

<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle"  BasedOn="{StaticResource BaseButtonStyle}">          
    <Setter Property="Background" Value="#1FA3EB"  />
    <Setter Property="Foreground" Value="#FFFFFF"  />
    <Setter Property="Content" >
        <Setter.Value>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="√" />
                <TextBlock Text="{TemplateBinding Button.Content}" />
            </StackPanel>
        </Setter.Value>
    </Setter>
</Style>

then i get this result: 然后我得到这个结果:

在此处输入图片说明

As you can see this is similar to the firstresult, where the check mark is missing 如您所见,这类似于第一个结果,其中没有选中标记

The look and functionality of the default Button is defined in the default ControlTemplate for the Button (you can find that in the Button Styles and Templates page on MSDN). 默认的外观和功能Button在默认定义ControlTemplateButton (你可以发现,在按钮样式和模板页面上MSDN)。 As you changed the ControlTemplate , the default look and behaviour is now gone and replaced with your plain StackPanel and TextBlock elements. 更改ControlTemplate ,默认外观和行为现在消失了,并替换为普通的StackPanelTextBlock元素。

When providing a new ControlTemplate for controls, it is a good idea to start with the default ControlTemplate and make small changes until you have your desired look. 为控件提供新的ControlTemplate ,最好从默认的ControlTemplate开始并进行一些小的更改,直到您具有所需的外观。 Here is a slight extension to your Style that provides some basic mouse over functionality: 这是对Style的略微扩展,提供了一些基本的鼠标悬停功能:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate>
            <Border BorderBrush="Black" BorderThickness="1" CornerRadius="3">
                <Border.Style>
                    <Style TargetType="{x:Type Border}">
                        <Setter Property="Background" Value="LightBlue" />
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="LightGreen" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="√" />
                <TextBlock Text="{TemplateBinding Button.Content}" />
                </StackPanel>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

You can find out more about ControlTemplate s from the ControlTemplate Class page on MSDN. 您可以从MSDN上的ControlTemplate页面找到有关ControlTemplate的更多信息。

If you only need to change the content of the button, you can do it this way: 如果只需要更改按钮的内容,则可以通过以下方式进行操作:

<Button Style="{StaticResource BaseButtonStyle}">
    <!-- This is the content -->
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="√" />
        <TextBlock Text="{Binding ButtonTextBinding}" />
    </StackPanel>
</Button>

This way you don't loose the basic styling. 这样,您就不会失去基本样式。 The button content property is an object, so it takes anything. button content属性是一个对象,因此它需要任何东西。 The moment you alter the control template of a button, everything about background, border, mouse over, pressed states etc. is lost. 更改按钮的控制模板后,有关背景,边框,鼠标悬停,按下状态等的所有信息都会丢失。 The best way to restyle any WPF control is to take the basic template from MSDN and modify it, rather than starting from scratch. 重设任何WPF控件样式的最佳方法是从MSDN中获取并修改基本模板,而不是从头开始。 You can find the button template here: 您可以在此处找到按钮模板:

.NET 4.5 Button template .NET 4.5按钮模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM