WPF 将焦点设置为 Combobox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20961303/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:26:08  来源:igfitidea点击:

WPF setting focus to Combobox

c#wpfcomboboxfocus

提问by michelqa

Just drop a button and a combobox inside a new WPF application

只需在新的 WPF 应用程序中放置一个按钮和一个组合框

On the button click simply set the focus to the combobox with a comboBox1.Focus() (or anything else possible)

在按钮上单击只需使用 comboBox1.Focus() (或任何其他可能的方法)将焦点设置到组合框

questions :

问题 :

1- When setting focus from code (with focus()) the combobox never receive the focus. why?

1- 从代码(使用 focus())设置焦点时,组合框永远不会收到焦点。为什么?

2- When navigating with the Tab key the combobox receive the focus and display a kind of focus selector around the control? is there any way to do the same (a real focus) from code?

2- 使用 Tab 键导航时,组合框接收焦点并在控件周围显示一种焦点选择器?有没有办法从代码中做同样的事情(真正的焦点)?

Thanks

谢谢

回答by FarmerBob

I think it is actually working - the problem is that in non-editable ComboBox it is hard to tell visually if the focus is there.

我认为它实际上是有效的 - 问题是在不可编辑的 ComboBox 中,很难直观地判断焦点是否在那里。

If you add IsEditable="True" you will see it the cursor will move to the ComboBox when you click the button.

如果您添加 IsEditable="True",您将看到当您单击按钮时,光标将移动到 ComboBox。

The focus selector you see with the Tab key is keyboard focus. Try this: on your app, without making ComboBox editable, navigate to the button and press SPACE. You will see the rectangle selection appear around the ComboBox, because the keyboard focus will move there.

您使用 Tab 键看到的焦点选择器是键盘焦点。试试这个:在你的应用程序上,不让 ComboBox 可编辑,导航到按钮并按空格键。您将看到 ComboBox 周围出现矩形选择,因为键盘焦点将移动到那里。

回答by Heena Patil

Focus style (dotted line ) is only appeared when we used Tab key ..thats why combobox is only taken focus not focus style on button click..but you can show focus by opening dropdown like below

焦点样式(虚线)仅在我们使用 Tab 键时才会出现。

     private void Button_Click_1(object sender, RoutedEventArgs e)
    {                              
         Cmb.Focus();
         Cmb.IsDropDownOpen = true;                                
    }

Sorry if I am wrong to your approach.

对不起,如果我对你的方法有误。

回答by sunnytyra

Read about selectionboxitem of the combobox which is responsible for selection in comobox edit area. Try setting the IsSelected along with the Focus property.

阅读组合框的 selectionboxitem ,它负责在组合框编辑区域中进行选择。尝试设置 IsSelected 和 Focus 属性。

There are lots of new things on focus handling as compared to Winforms where it was just select and focus. Check the API:

与只是选择和聚焦的 Winforms 相比,焦点处理有很多新东西。检查 API:

http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox%28v=vs.110%29.aspx

回答by Anthony Hayward

If you have restyled the combobox, be careful with "OverridesDefaultStyle". Setting this to true can hide the dotted focus rectangle.

如果您重新设计了组合框,请注意“OverridesDefaultStyle”。将此设置为 true 可以隐藏虚线焦点矩形。