WPF按钮问题与风格

时间:2010-10-14 21:35:26

标签: wpf button

我必须创建一个自定义样式按钮。问题是,虽然我在鼠标移动时或在有焦点时改变了所有内容,但它会获得原始颜色! 试图设置FocusVisualStyle="{x:Null}",但它一直在做....

<Button Content="Button" Height="143" Margin="85,76,190,0" VerticalAlignment="Top" FocusVisualStyle="{x:Null}" Background="#FFE9D7D7"/>

我能做什么?

3 个答案:

答案 0 :(得分:3)

您看到的视觉效果可能来自默认控件模板,其中包含窗口镶边。您可能想尝试为按钮创建自定义模板,这将为您提供对视觉元素的完全控制。

答案 1 :(得分:1)

即使您定义了自定义样式,如果未在自定义样式中设置它们,该按钮仍会继承默认样式的某些属性。所以你有两个选择:

  • 在按钮上设置OverridesDefaultStyletrue,以便它不会继承默认样式
  • 在自定义样式
  • 中明确设置背景/边框/前景画笔

答案 2 :(得分:1)

默认按钮模板覆盖您的样式。所以你已经为按钮创建了自己的控件模板。这是一个例子。

 <Style x:Key="InformButton" TargetType="Button">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Margin" Value="2"/>
        <Setter Property="FontFamily" Value="Verdana"/>
        <Setter Property="FontSize" Value="11px"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <!--<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisual}" />-->
        <Setter Property="Background" >
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
                    <GradientStop Color="#FFFFD190" Offset="0.2"/>
                    <GradientStop Color="Orange" Offset="0.85"/>
                    <GradientStop Color="#FFFFD190" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border" 
            BorderThickness="1"
            Padding="4,2" 
            BorderBrush="DarkGray" 
            CornerRadius="3" 
            Background="{TemplateBinding Background}">
                        <Grid >
                            <ContentPresenter HorizontalAlignment="Center" 
                           VerticalAlignment="Center" Name="contentShadow" 
                >
                                <ContentPresenter.RenderTransform>
                                    <TranslateTransform X="1.0" Y="1.0" />
                                </ContentPresenter.RenderTransform>
                            </ContentPresenter>
                            <ContentPresenter HorizontalAlignment="Center" 
                        VerticalAlignment="Center" Name="content"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="#FF4788c8" />
                            <Setter Property="Foreground" Value="#FF4788c8" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" >
                                <Setter.Value>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
                                        <GradientStop Color="#FFFFD190" Offset="0.35"/>
                                        <GradientStop Color="Orange" Offset="0.95"/>
                                        <GradientStop Color="#FFFFD190" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter TargetName="content" Property="RenderTransform" >
                                <Setter.Value>
                                    <TranslateTransform Y="1.0" />
                                </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>