0

我正在尝试使用控件模板来自定义 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>

请指教 谢谢

4

1 回答 1

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>
于 2012-07-31T00:49:03.747 回答