2

我已经为我的 WPF 应用程序中的按钮创建了一种样式。

然而,在 Vista 上,当一个按钮被聚焦时,它会发出一种淡蓝色的脉动,这在设计中并不好看。

如何覆盖这种自动脉动?

<Style x:Key="NavigationButtonStyle" TargetType="Button">
    <Setter Property="Width" Value="400"/>
    <Setter Property="Height" Value="100"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="FontSize" Value="56"/>
    <Setter Property="Foreground" Value="#aaa"/>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="#ddd" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Foreground" Value="#aaa" />
        </Trigger>
    </Style.Triggers>
</Style>

试过“IsFocused”:

添加这对 Vista 没有任何影响,按钮看起来仍然相同,并且在聚焦时在 Vista 中脉动:

<Trigger Property="IsFocused" Value="True">
    <Setter Property="Background" Value="Red" />
    <Setter Property="Foreground" Value="Blue" />
</Trigger>

尝试过“ControlTemplate”解决方案:

仍然没有效果:

<Style x:Key="ButtonFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle SnapsToDevicePixels="true" Margin="2" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="NavigationButtonStyle" TargetType="Button">
    <Setter Property="Width" Value="400"/>
    <Setter Property="Height" Value="100"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="FontSize" Value="56"/>
    <Setter Property="Foreground" Value="#aaa"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="#ddd" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Foreground" Value="#aaa" />
        </Trigger>
    </Style.Triggers>
</Style>
4

3 回答 3

1

此效果是为响应 IsKeyboardFocused 而设置的,而不仅仅是标准焦点。

在 Blend 中,如果您编辑标准按钮的控件模板,您将看到 IsKeyboardFocused=true 触发器负责此效果。

<Style x:Key="Button_NonGlow" 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}">
                    <Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true" x:Name="Chrome" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}">
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
                    </Microsoft_Windows_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>
于 2009-05-18T17:58:48.790 回答
0

您正在寻找的是 IsFocused 触发器属性。抱歉,我在这里没有适合您的工作示例。


更新:

我找到了我曾经使用过的一小段代码。我不确定是否有效并且我无法测试它(我在 XP 盒子上):

<Style x:Key="ButtonFocusVisual">
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate>
                <Rectangle SnapsToDevicePixels="true" Margin="2" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type Button}">
    <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
</Style>    
于 2009-05-18T14:55:52.763 回答
0

根据您的具体要求,您可以通过设置 IsTabStop="False" 和 IsDefault="False" 来确保按钮永远不会获得键盘焦点。

于 2013-02-04T20:35:00.147 回答