如何在 C# 中的按钮上设置/更改/删除焦点样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/148729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set/change/remove focus style on a Button in C#?
提问by
I have a couple of buttons of which I modified how they look. I have set them as flat buttons with a background and a custom border so they look all pretty and nothing like normal buttons anymore (actually, they look like Office 2003 buttons now ;-). The buttons have a border of one pixel.
我有几个按钮,我修改了它们的外观。我将它们设置为带有背景和自定义边框的平面按钮,因此它们看起来都很漂亮,不再像普通按钮(实际上,它们现在看起来像 Office 2003 按钮;-)。按钮有一个像素的边框。
However when the button gets selected (gets the focus through either a click or a keyboard action like pressing the tab key) the button suddenly gets and extra border around it of the same colour, so making it a two pixel border. Moreover when I disable the one pixel border, the button does not get a one pixel border on focus.
但是,当按钮被选中(通过单击或键盘操作(如按下 Tab 键)获得焦点)时,按钮会突然获得相同颜色的额外边框,因此使其成为两个像素的边框。此外,当我禁用一像素边框时,按钮不会在焦点上获得一像素边框。
On the net this question is asked a lot like 'How can I disable focus on a Button', but that's not what I want: the focus should still exist, just not displayin the way it does now.
在网上这个问题很像“我如何禁用按钮上的焦点”,但这不是我想要的:焦点应该仍然存在,只是不以现在的方式显示。
Any suggestions? :-)
有什么建议?:-)
回答by Orion Adrian
Certainly you can draw the button yourself. One of the state flags is focused.
当然,您可以自己绘制按钮。其中一个状态标志是聚焦的。
So on the draw event if the flag is focused go ahead and draw the button how you like, otherwise just pass it on to the base method.
所以在绘制事件上,如果标志被聚焦,继续绘制你喜欢的按钮,否则只需将它传递给基本方法。
回答by Jeff Yates
Consider implementing your own drawing code for the button. That way you have full control. In the past, I've implemented my own Control derivative that custom paints my button and implements all the button characteristics for my purposes, but you should be able to override the button's painting and do it yourself, thereby controlling how it draws in every state, including when focused.
考虑为按钮实现自己的绘图代码。这样你就可以完全控制。过去,我已经实现了我自己的 Control 派生,它自定义绘制我的按钮并为我的目的实现所有按钮特性,但是您应该能够覆盖按钮的绘制并自己完成,从而控制它在每个状态下的绘制方式,包括聚焦时。
回答by Michael L Perry
Is this the effect you are looking for?
这是您要找的效果吗?
public class NoFocusCueButton : Button
{
protected override bool ShowFocusCues
{
get
{
return false;
}
}
}
You can use this custom button class just like a regular button, but it won't give you an extra rectangle on focus.
您可以像使用常规按钮一样使用此自定义按钮类,但它不会为您提供额外的焦点矩形。
回答by John Rudy
The second border which gets added is the Windows standard "default button" border. You may have noticed that if you tab through most dialog boxes with multiple buttons (such as any Control Panel properties window), the original "double-bordered" button becomes "normal," and the in-focus button becomes "double-bordered."
添加的第二个边框是 Windows 标准的“默认按钮”边框。您可能已经注意到,如果您使用 Tab 键浏览大多数带有多个按钮的对话框(例如任何控制面板属性窗口),则原来的“双边框”按钮变为“普通”,焦点按钮变为“双边框”。 ”
This isn't necessarily focus at work, but rather a visual indication of the action undertaken by hitting the Enter key.
这不一定是专注于工作,而是通过按 Enter 键进行的操作的视觉指示。
It sounds, to me, like you don't really care about that internal working. You want the display to not have two borders -- totally understandable. The internal working is to explain why you're seeing this behavior. Now ... To try and fix it.
听起来,对我来说,你并不真正关心内部工作。您希望显示器没有两个边框——完全可以理解。内部工作是解释为什么您会看到这种行为。现在......尝试修复它。
The first thing I'd try -- and bear in mind, I haven't validated this -- is a hack. When a button receives focus (thereby getting the double-border), turn off your single border. You might get the effect you want, and it's pretty simple. (Hook into the Focus event. Even better, subclass Button and override OnFocus, then use that subclass for your future buttons.)
我要尝试的第一件事——记住,我还没有验证过——是一个黑客。当一个按钮获得焦点(从而获得双边框)时,关闭你的单边框。你可能会得到你想要的效果,而且很简单。(连接到 Focus 事件。更好的是,子类化 Button 并覆盖 OnFocus,然后将该子类用于您未来的按钮。)
However, that might introduce new, awkward visual side effects. In that vein -- and because hacks are rarely the best answer -- I have to "officially" recommend what others have said: Custom paint the button. Although the code here may be overkill, this link at CodeProjectdiscusses how to do that (VB link; you'll need translate). You should, in a full-on custom mode, be able to get rid of that second border completely.
然而,这可能会引入新的、尴尬的视觉副作用。在这种情况下——而且因为黑客很少是最好的答案——我必须“正式”推荐其他人所说的话:自定义绘制按钮。虽然这里的代码可能有点过分,但CodeProject 上的这个链接讨论了如何做到这一点(VB 链接;您需要翻译)。在完全自定义模式下,您应该能够完全摆脱第二个边框。
回答by Ryan O'Neill
There is another way which works well for flat styled buttons. Don't use buttons but labels. As you are completely replacing the UI for the button it does not matter whether your use a button control or a label. Just handle the click in the same way.
还有另一种方式适用于平面样式的按钮。不要使用按钮,而是使用标签。当您完全替换按钮的 UI 时,使用按钮控件还是标签都没有关系。只需以相同的方式处理点击。
This worked for me, although not great practice it is a good hack and as long as you name the button obviously (and comment the source) other coders will pick up the idea.
这对我有用,虽然不是很好的练习,但它是一个很好的技巧,只要你明显地命名按钮(并注释源),其他编码人员就会接受这个想法。
Ryan
瑞安
回答by Pop Catalin
Set the FocusVisualStyledependency property to null in your style, and the dotted border will be gone.
在您的样式中将FocusVisualStyle依赖项属性设置为 null,虚线边框将消失。
From MSDN: Styling for Focus in Controls, and FocusVisualStyle
来自 MSDN:控件中的焦点样式和 FocusVisualStyle
Windows Presentation Foundation (WPF) provides two parallel mechanisms for changing the visual appearance of a control when it receives keyboard focus. The first mechanism is to use property setters for properties such as IsKeyboardFocused within the style or template that is applied to the control. The second mechanism is to provide a separate style as the value of the FocusVisualStyle property; the "focus visual style" creates a separate visual tree for an adorner that draws on top of the control,rather than changing the visual tree of the control or other UI element by replacing it. This topic discusses the scenarios where each of these mechanisms is appropriate.
Windows Presentation Foundation (WPF) 提供了两种并行机制,用于在控件接收键盘焦点时更改控件的视觉外观。第一种机制是对应用于控件的样式或模板中的 IsKeyboardFocused 等属性使用属性设置器。Ť他第二机构是提供一个单独的样式作为FocusVisualStyle属性的值; “焦点视觉样式”为在控件顶部绘制的装饰器创建单独的视觉树,而不是通过替换来更改控件或其他 UI 元素的视觉树。本主题讨论其中每种机制都适用的场景。
The extra borderyou see is defined by the FocusVisualStyle and not in the control template, so you need to remove or override the style to remove the border.
您看到的额外边框是由 FocusVisualStyle 定义的,而不是在控件模板中,因此您需要移除或覆盖样式以移除边框。
回答by Pop Catalin
Another option (although a bit hacktastic) is to attach an event-handler to the button's GotFocus event. In that event-handler, pass a value of False to the button's NotifyDefault()
method. So, for instance:
另一种选择(虽然有点骇人听闻)是将事件处理程序附加到按钮的 GotFocus 事件。在该事件处理程序中,将 False 值传递给按钮的NotifyDefault()
方法。因此,例如:
void myButton_GotFocus(object sender, EventArgs e)
{
myButton.NotifyDefault(false);
}
I'm assuming this will work every time, but I haven't tested it extensively. It's working for me for now, so I'm satisfied with that.
我假设这每次都会起作用,但我还没有对其进行广泛的测试。它现在对我有用,所以我很满意。
回答by Pop Catalin
If you have a textbox and a button
then on textchange event of textbox
write button1.focus();
如果您有一个文本框和一个按钮,则在文本框写入的 textchange 事件上 button1.focus();
It will work.
它会起作用。
回答by Marcus Rex
Make a custom button:
制作自定义按钮:
public partial class CustomButton: Button
{
public ButtonPageButton()
{
InitializeComponent();
this.SetStyle(ControlStyles.Selectable, false);
}
}
That'll get rid of that annoying border! ;-)
这将摆脱烦人的边框!;-)
回答by ketchup201
I've had good luck merely setting the Focusable property of the button to be false:
我很幸运,只是将按钮的 Focusable 属性设置为 false:
<Button HorizontalAlignment="Left" Margin="0,2"
Command="{Binding OpenSuspendedJobCommand, Mode=OneWay}"
Focusable="False"
Style="{StaticResource ActionButton}" Content="Open Job..." />