Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 自定义按钮样式不起作用?_Wpf_Xaml_Styles - Fatal编程技术网

Wpf 自定义按钮样式不起作用?

Wpf 自定义按钮样式不起作用?,wpf,xaml,styles,Wpf,Xaml,Styles,我试图设置一个按钮的样式,但当我应用自定义样式时,它最终不会显示,只有它的内容(按钮的文本)可见。这就是我如何声明我的风格: <Color x:Key="NormalColor" A="255" R="60" G ="158" B="184"/> <Color x:Key="HoverColor" A="255" R="74" G ="177" B="204"/> <Color x:Key="PressedColor" A="255" R="

我试图设置一个按钮的样式,但当我应用自定义样式时,它最终不会显示,只有它的内容(按钮的文本)可见。这就是我如何声明我的风格:

    <Color x:Key="NormalColor" A="255" R="60" G ="158" B="184"/>
    <Color x:Key="HoverColor" A="255" R="74" G ="177" B="204"/>
    <Color x:Key="PressedColor" A="255" R="26" G ="115" B="138"/>

    <SolidColorBrush x:Key="NormalBrush" Color="{StaticResource NormalColor}" />
    <SolidColorBrush x:Key="HoverBrush" Color="{StaticResource HoverColor}" />
    <SolidColorBrush x:Key="PressedBrush" Color="{StaticResource PressedColor}" />

    <Style x:Key="FlatButton" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="FontFamily" Value="Segoe UI"/>
        <Setter Property="Background" Value="{StaticResource NormalBrush}"/>
        <Setter Property="Foreground" Value="#000000"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">

                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="Content"/>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background"  Value="{StaticResource HoverBrush}" />
                            <Setter Property="Foreground" Value="#000000" />
                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{StaticResource PressedBrush}" />
                            <Setter Property="Foreground" Value="#000000" />
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="{StaticResource NormalBrush}" />
                            <Setter Property="Foreground" Value="#000000" />
                            <Setter Property="Opacity" Value="0.5" />                                
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>


下面是我如何使用它:

<Button Content="Connect" Command="model:GlobalCommands.ConnectToDBCommand" Style="{StaticResource FlatButton}"/>

谁能告诉我这件事哪里出错了?
谢谢大家!

您的
控制模板中没有使用
背景的内容。将您的
ContentPresenter
放入
边框中(例如),并使用
TemplateBinding
绑定其
背景

<Border Background="{TemplateBinding Background}">
    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="Content"/>
</Border>

添加到@dkozl的答案中,您应该将
TemplateBinding
s添加到
按钮内的任何属性。ControlTemplate
,您可能希望从
按钮外部设置。ControlTemplate

<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding
    BorderThickness}" Background="{TemplateBinding Background}">
    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>

然后你可以这样做:

<Button Content="Connect" Command="model:GlobalCommands.ConnectToDBCommand" 
    Style="{StaticResource FlatButton}" BorderBrush="Blue" BordeThickness="5" />