When a standard WPF button is clicked, it gets highlighted in blue (probably using the blue color from whatever Windows theme is set), and it stays highlighted until you interact with any other control. For my application, it is confusing for the user.
当一个标准的WPF按钮被点击时,它会以蓝色高亮显示(可能会使用来自任何窗口主题的蓝色),并且它会一直高亮显示,直到你与其他控件交互为止。对于我的应用程序,用户会感到困惑。
Is there a simple way turn this off and get the button back to its normal style? I am using .NET 4.
是否有一种简单的方法将其关闭并使按钮恢复到正常样式?我正在使用。net 4。
5 个解决方案
#1
47
What happens is that the button accepts the input focus after it has been clicked, just like any other control does when you click on it.
所发生的情况是,按钮在被单击后接受输入焦点,就像您单击它时其他控件所做的那样。
And the way that Windows indicates that a control has the input focus (at least under the Aero theme) is with a subtle blue highlight.
窗口显示控件有输入焦点(至少在Aero主题下)的方式是使用一个微妙的蓝色高光。
For a button control in particular, when it has the input focus, simply pressing the Enter key will "push" that button. That's why maintaining the highlight is very important, so that the user knows what to expect.
特别是对于按钮控件,当它有输入焦点时,只需按下Enter键就会“按”那个按钮。这就是为什么保持突出显示是非常重要的,这样用户就知道会发生什么。
The better solution is to set the focus to a different control in your window immediately after the user has clicked on the button. That way, it will no longer be automatically highlighted and no action will be automatically triggered when the user presses the Enter key. (This is the real usability problem that you're trying to solve, even if you don't know it yet. Nothing is more confusing than a button inadvertently getting clicked when the user is actually trying to type something.)
更好的解决方案是在用户单击按钮后立即将焦点设置为窗口中的另一个控件。这样,当用户按下Enter键时,将不再自动突出显示它,也不会自动触发任何操作。(这是你正在尝试解决的真正可用性问题,即使你还不知道。最让人困惑的是,当用户试图输入某样东西时,一个按钮被不经意地点击了。
You could prevent the button from ever getting the focus altogether by setting its Focusable
property to false, but I would very much recommend against this. Once you've done this, there will be no way for the user to "press" the button using only the keyboard. Well-designed applications should always be accessible to users who either prefer not to or who are unable to use the mouse.
您可以通过将它的可调焦属性设置为false来防止按钮完全获得焦点,但是我强烈建议不要这样做。一旦你这样做了,用户就没有办法只用键盘“按”按钮。设计良好的应用程序应该始终对不喜欢或不能使用鼠标的用户开放。
#2
10
Try to set Focusable
to false. The button will be clickable but will not remain focused.
试着将focable设置为false。该按钮将会被点击,但不会继续聚焦。
#3
2
That is simply the focussed state. To turn it off you will have to change the focussed state. It's easiest by using Blend.
那只是集中的状态。要关闭它,你必须改变焦点状态。使用混合液是最简单的。
I do not recommend setting Focusable to false because it interferes with using the keyboard
我不建议将focable设置为false,因为它会干扰键盘的使用
#4
2
This is the default look for Aero buttons when they have focus. You can either set Focusable="False"
or use a custom Style, that doesn't render it differently when the button has focus. Something like:
这是当Aero按钮有焦点时的默认外观。您可以设置focapplicable ="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}" />
You'd need to add a reference to PresentationFramework.Aero.dll
您需要添加对presentationframe . aero. dll的引用
#5
0
I needed to do something similar, but in code at runtime, it looked like this
我需要做一些类似的事情,但是在运行时的代码中,看起来是这样的
//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 };
You can see that i comment the ""RenderMouseOver" line
您可以看到我注释了“RenderMouseOver”行
My first hope was using FrameworkElementFactory but i needed to create all the default template.... ALL BY HAND! ;)
Using
我第一次希望使用FrameworkElementFactory但我需要创建所有默认模板....所有手工!,)使用
System.Windows.Markup.XamlWriter.Save(myButton.Template)
It give me the template i wanted and then removing the Render section was easy.
它给了我想要的模板,然后删除渲染部分很容易。
#1
47
What happens is that the button accepts the input focus after it has been clicked, just like any other control does when you click on it.
所发生的情况是,按钮在被单击后接受输入焦点,就像您单击它时其他控件所做的那样。
And the way that Windows indicates that a control has the input focus (at least under the Aero theme) is with a subtle blue highlight.
窗口显示控件有输入焦点(至少在Aero主题下)的方式是使用一个微妙的蓝色高光。
For a button control in particular, when it has the input focus, simply pressing the Enter key will "push" that button. That's why maintaining the highlight is very important, so that the user knows what to expect.
特别是对于按钮控件,当它有输入焦点时,只需按下Enter键就会“按”那个按钮。这就是为什么保持突出显示是非常重要的,这样用户就知道会发生什么。
The better solution is to set the focus to a different control in your window immediately after the user has clicked on the button. That way, it will no longer be automatically highlighted and no action will be automatically triggered when the user presses the Enter key. (This is the real usability problem that you're trying to solve, even if you don't know it yet. Nothing is more confusing than a button inadvertently getting clicked when the user is actually trying to type something.)
更好的解决方案是在用户单击按钮后立即将焦点设置为窗口中的另一个控件。这样,当用户按下Enter键时,将不再自动突出显示它,也不会自动触发任何操作。(这是你正在尝试解决的真正可用性问题,即使你还不知道。最让人困惑的是,当用户试图输入某样东西时,一个按钮被不经意地点击了。
You could prevent the button from ever getting the focus altogether by setting its Focusable
property to false, but I would very much recommend against this. Once you've done this, there will be no way for the user to "press" the button using only the keyboard. Well-designed applications should always be accessible to users who either prefer not to or who are unable to use the mouse.
您可以通过将它的可调焦属性设置为false来防止按钮完全获得焦点,但是我强烈建议不要这样做。一旦你这样做了,用户就没有办法只用键盘“按”按钮。设计良好的应用程序应该始终对不喜欢或不能使用鼠标的用户开放。
#2
10
Try to set Focusable
to false. The button will be clickable but will not remain focused.
试着将focable设置为false。该按钮将会被点击,但不会继续聚焦。
#3
2
That is simply the focussed state. To turn it off you will have to change the focussed state. It's easiest by using Blend.
那只是集中的状态。要关闭它,你必须改变焦点状态。使用混合液是最简单的。
I do not recommend setting Focusable to false because it interferes with using the keyboard
我不建议将focable设置为false,因为它会干扰键盘的使用
#4
2
This is the default look for Aero buttons when they have focus. You can either set Focusable="False"
or use a custom Style, that doesn't render it differently when the button has focus. Something like:
这是当Aero按钮有焦点时的默认外观。您可以设置focapplicable ="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}" />
You'd need to add a reference to PresentationFramework.Aero.dll
您需要添加对presentationframe . aero. dll的引用
#5
0
I needed to do something similar, but in code at runtime, it looked like this
我需要做一些类似的事情,但是在运行时的代码中,看起来是这样的
//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 };
You can see that i comment the ""RenderMouseOver" line
您可以看到我注释了“RenderMouseOver”行
My first hope was using FrameworkElementFactory but i needed to create all the default template.... ALL BY HAND! ;)
Using
我第一次希望使用FrameworkElementFactory但我需要创建所有默认模板....所有手工!,)使用
System.Windows.Markup.XamlWriter.Save(myButton.Template)
It give me the template i wanted and then removing the Render section was easy.
它给了我想要的模板,然后删除渲染部分很容易。