WPF XAML:按钮上的边框画笔,IsDefault =" True"

时间:2017-11-29 01:40:13

标签: c# wpf xaml

当我尝试使用IsDefault =" True"在按钮上输入边框画笔颜色时属性,边界不会改变并保留IsDefault属性的蓝色边框。

如果我删除IsDefault =" True",则应用边框画笔颜色。

有没有办法让按钮成为默认按钮并为其提供自定义边框颜色?

2 个答案:

答案 0 :(得分:1)

问题是按钮ControlTemplate,它会在某些情况下更改按钮外观。

查看默认按钮的样式/控件模板:

<Style x:Key="ButtonFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
    <GradientStop Color="#F3F3F3" Offset="0"/>
    <GradientStop Color="#EBEBEB" Offset="0.5"/>
    <GradientStop Color="#DDDDDD" Offset="0.5"/>
    <GradientStop Color="#CDCDCD" Offset="1"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
    <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Themes:ButtonChrome>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                    </Trigger>
                    <Trigger Property="ToggleButton.IsChecked" Value="true">
                        <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#ADADAD"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

相关部分可能是

<Themes:ButtonChrome x:Name="Chrome"
    ...
    RenderMouseOver="{TemplateBinding IsMouseOver}"
    RenderPressed="{TemplateBinding IsPressed}"
    RenderDefaulted="{TemplateBinding IsDefaulted}"
    ... >

并且控件模板触发

<Trigger Property="IsKeyboardFocused" Value="true">
    <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
</Trigger>
<Trigger Property="ToggleButton.IsChecked" Value="true">
    <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
</Trigger>

因此,对于您的特定情况,如果您真的只想要IsDefault的不同边框,但是您想要所有边框更改为焦点,鼠标悬停和其他状态,那么您可以通过设置{{1来修改控件模板在RenderDefaulted="False"(而非Themes:ButtonChrome)。

答案 1 :(得分:0)

方法1:最好的方法是在Window.Resources中定义一种样式,将该样式应用于所有按钮:

<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="BorderBrush" Value="Blue" />
    </Style>
</Window.Resources>

方法2: 另一种方法是将x:Key="yourButtonStyle"添加到Style元素,并将其分配给Style属性中所需的任何按钮:

<Window.Resources>
    <Style x:Key="yourButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="BorderBrush" Value="Blue" />
    </Style>
</Window.Resources>
<Button Style="{StaticResource yourButtonStyle}"/>