单击后如何防止WPF按钮保持突出显示?

时间:2011-05-10 12:53:02

标签: c# .net wpf button focus

单击标准WPF按钮时,它会以蓝色突出显示(可能使用设置的任何Windows主题中的蓝色),并且在您与任何其他控件交互之前,它会一直保持突出显示。对于我的应用程序,它对用户来说很困惑。

是否有一种简单的方法可以将其关闭并使按钮恢复正常风格? 我使用的是.NET 4。

7 个答案:

答案 0 :(得分:49)

按钮在点击后接受输入焦点会发生什么情况,就像点击它时任何其他控件一样。

Windows指示控件具有输入焦点的方式(至少在Aero主题下)具有微妙的蓝色突出显示。

特别是对于按钮控件,当它具有输入焦点时,只需按 Enter 键即可“按下”该按钮。这就是保持亮点非常重要的原因,以便用户知道会发生什么。

更好的解决方案是在用户点击按钮后立即将焦点设置到窗口中的其他控件。这样,它将不再自动突出显示,并且当用户按下 Enter 键时不会自动触发任何操作。 (这是你试图解决的真正的可用性问题,即使你还不知道它。当用户实际尝试输入时,没有什么比无意中被点击的按钮更令人困惑的了。东西。)

可能通过将其Focusable property设置为false来阻止该按钮完全获得焦点,但我非常推荐这一点。一旦完成此操作,用户将无法仅使用键盘“按下”按钮。精心设计的应用程序总是可供不喜欢或不能使用鼠标的用户访问。

答案 1 :(得分:10)

尝试将Focusable设置为false。该按钮可以点击,但不会保持聚焦状态。

答案 2 :(得分:2)

这是Aero按钮具有焦点时的默认外观。您可以设置Focusable="False"或使用自定义样式,当按钮具有焦点时,该样式不会以不同方式呈现。类似的东西:

xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
<Style x:Key="BaseButtonStyle" TargetType="{x:Type ButtonBase}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <theme:ButtonChrome Name="Chrome" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding Button.IsDefaulted}"
                        RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"
                        SnapsToDevicePixels="true">
                    <ContentPresenter Margin="{TemplateBinding Padding}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </theme:ButtonChrome>
                <ControlTemplate.Triggers>
                    <!--
                    Do not show blue when focused
                    <Trigger Property="IsKeyboardFocused" Value="true">
                        <Setter TargetName="Chrome" Property="RenderDefaulted" Value="true" />
                    </Trigger>-->
                    <Trigger Property="ToggleButton.IsChecked" Value="true">
                        <Setter TargetName="Chrome" Property="RenderPressed" Value="true" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#ADADAD" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="{x:Type ToggleButton}" BasedOn="{StaticResource BaseButtonStyle}" TargetType="{x:Type ToggleButton}" />
<Style x:Key="{x:Type RepeatButton}" BasedOn="{StaticResource BaseButtonStyle}" TargetType="{x:Type RepeatButton}" />
<Style x:Key="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}" TargetType="{x:Type Button}" />

您需要添加对PresentationFramework.Aero.dll的引用

答案 3 :(得分:2)

这只是关注焦点的状态。要将其关闭,您必须更改聚焦状态。使用Blend最简单。

我不建议将Focusable设置为false,因为它会干扰使用键盘

答案 4 :(得分:0)

我需要做类似的事情,但在运行时的代码中,它看起来像这样

//You can get this XAML by using System.Windows.Markup.XamlWriter.Save(yourButton.Template)";
             const string controlXaml = "<ControlTemplate TargetType=\"ButtonBase\" " +
                                    "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
                                    "xmlns:s=\"clr-namespace:System;assembly=mscorlib\" " +
                                    "xmlns:mwt=\"clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero\">" +
                                    "<mwt:ButtonChrome Background=\"{TemplateBinding Panel.Background}\" " +
                                    "BorderBrush=\"{TemplateBinding Border.BorderBrush}\" " +
                                    "RenderDefaulted=\"{TemplateBinding Button.IsDefaulted}\" " +
                                    //"RenderMouseOver=\"{TemplateBinding UIElement.IsMouseOver}\" " +
                                    "RenderPressed=\"{TemplateBinding ButtonBase.IsPressed}\" Name=\"Chrome\" SnapsToDevicePixels=\"True\">" +
                                    "<ContentPresenter RecognizesAccessKey=\"True\" " +
                                    "Content=\"{TemplateBinding ContentControl.Content}\" " +
                                    "ContentTemplate=\"{TemplateBinding ContentControl.ContentTemplate}\" " +
                                    "ContentStringFormat=\"{TemplateBinding ContentControl.ContentStringFormat}\" " +
                                    "Margin=\"{TemplateBinding Control.Padding}\" " +
                                    "HorizontalAlignment=\"{TemplateBinding Control.HorizontalContentAlignment}\" " +
                                    "VerticalAlignment=\"{TemplateBinding Control.VerticalContentAlignment}\" " +
                                    "SnapsToDevicePixels=\"{TemplateBinding UIElement.SnapsToDevicePixels}\" /></mwt:ButtonChrome>" +
                                    "<ControlTemplate.Triggers>" +
                                    "<Trigger Property=\"UIElement.IsKeyboardFocused\">" +
                                    "<Setter Property=\"mwt:ButtonChrome.RenderDefaulted\" TargetName=\"Chrome\"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter>" +
                                    "<Trigger.Value><s:Boolean>True</s:Boolean></Trigger.Value></Trigger>" +
                                    "<Trigger Property=\"ToggleButton.IsChecked\">" +
                                    "<Setter Property=\"mwt:ButtonChrome.RenderPressed\" TargetName=\"Chrome\"><Setter.Value><s:Boolean>True</s:Boolean></Setter.Value></Setter>" +
                                    "<Trigger.Value><s:Boolean>True</s:Boolean></Trigger.Value></Trigger>" +
                                    "<Trigger Property=\"UIElement.IsEnabled\"><Setter Property=\"TextElement.Foreground\"><Setter.Value><SolidColorBrush>#FFADADAD</SolidColorBrush></Setter.Value></Setter>" +
                                    "<Trigger.Value><s:Boolean>False</s:Boolean></Trigger.Value></Trigger></ControlTemplate.Triggers>" +
                                    "</ControlTemplate>";

        var xamlStream = new MemoryStream(System.Text.Encoding.Default.GetBytes(controlXaml));
        var _buttonControlTemplate = (ControlTemplate)System.Windows.Markup.XamlReader.Load(xamlStream);
        var yourButton = new Button() { Template = _buttonControlTemplate };

你可以看到我评论“”RenderMouseOver“行

我的第一个希望是使用FrameworkElementFactory,但我需要创建所有默认模板.... ALL BY HAND! ;)
使用

System.Windows.Markup.XamlWriter.Save(myButton.Template)

它给了我想要的模板,然后删除渲染部分很简单。

答案 5 :(得分:0)

我找到了两步解决方案。该解决方案有点有趣,但可以解决。 我的推荐职位是;

How to remove global FocusVisualStyle to all the Controls?

C# WPF application .NET 4.5 Set Mouse Position

必须导入一个DLL,因为WPF不直接支持鼠标光标移动。

  1. 将System.Runtime.InteropServices添加到名称空间
  2. 在MainWindow或窗口的任何代码中添加两行

[DllImport("User32.dll")] private static extern bool SetCursorPos(int X, int Y);

  1. 将这两行添加到点击事件中。 SetCursorPos(0, 0); ButtonName.FocusVisualStyle = null;

对我有用。

答案 6 :(得分:0)

<Style x:Key="TouchButton" TargetType="{x:Type Button}">
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="ClickMode" Value="Press"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="Border" CornerRadius="2" BorderThickness="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}">
                        <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Button.IsMouseOver" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="Red"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
相关问题