WPF按钮样式

时间:2012-07-30 23:47:23

标签: .net wpf xaml styles

我正在尝试使用控件模板来自定义WPF中的按钮。 我有以下问题: 1.在MouseOver上,按钮的颜色不会变为红色 2.按下时会产生摆动效果

下面是xaml:                                                            - >                     

                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Name="border"
                           BorderThickness="1"
                           Padding="4,2"
                           BorderBrush="DarkGray"
                           CornerRadius="3"
                           Background="{StaticResource ButtonDarkColorBrush}">
                                <Grid >

                                    <ContentPresenter HorizontalAlignment="Center"
                                       VerticalAlignment="Center" Name="content" Style="{StaticResource ShadowStyle}"/>
                                </Grid>

                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <!--<Setter TargetName="border" Property="BorderBrush" Value="#FF4788c8" />-->
                                    <Setter Property="Foreground" Value="#FF4788c8" />
                                    <Setter Property="Background" Value="Red" />
                                </Trigger>


                                <Trigger Property="IsPressed" Value="True">
                                    <Setter TargetName="content" Property="RenderTransform" >
                                        <Setter.Value>
                                            <TranslateTransform Y="0.1" />
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                                <Trigger Property="IsDefaulted" Value="True">
                                    <Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
                                </Trigger>
                                <Trigger Property="IsFocused" Value="True">
                                    <Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter TargetName="border" Property="Opacity" Value="0.7" />
                                    <Setter Property="Foreground" Value="Gray" />
                                </Trigger>
                            </ControlTemplate.Triggers>

                        </ControlTemplate>
                    </Setter.Value>

                </Setter>
            </Style>

请指教 感谢

1 个答案:

答案 0 :(得分:2)

嗨Melspring试试这个代码对我有用。

<Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border" 
                BorderThickness="2"
                Padding="4,2" 
                BorderBrush="{DynamicResource BlueGradient2}"
                CornerRadius="5" 
                Background="{TemplateBinding Background}">
                        <Grid >
                            <ContentPresenter 
                            HorizontalAlignment="Center" 
                            VerticalAlignment="Center" 
                            Name="content"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Red"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="BorderBrush" Value="Red"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="Red"/>
                            <Setter Property="BorderBrush" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
            </Setter>