wpf 从键盘禁用整个用户控制聚焦
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18062111/
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
Disable whole user control focusing from keyboard
提问by Evil Beaver
I have UserControl in a window. When user walks window with "Tab" key user control gets focused and dashed border drawn around it. How to prevent this behavior?

我在一个窗口中有 UserControl。当用户使用“Tab”键移动窗口时,用户控件将获得焦点并在其周围绘制虚线边框。如何防止这种行为?

采纳答案by Evil Beaver
It was my mistake. I had xaml:
这是我的错误。我有xaml:
<ContentControl>
<ScrollViewer name="viewport"/>
</ContentControl>
and "viewport.Content" was set to my UserControl from code-behind.
并且“viewport.Content”从代码隐藏设置为我的 UserControl。
It was a ContentControl who draw the focus border. I removed it and left only a . Problem solved.
它是一个绘制焦点边框的 ContentControl。我删除了它,只留下了一个 . 问题解决了。
回答by Anatoliy Nikolaev
Try it for an control set Focusable = "False". Example:
试试它的控制集Focusable = "False"。例子:
<Grid Focusable="False">
...
</Grid>
Or set the Styleto focus yourself:
或者将 设置Style为专注于自己:
<Grid FocusVisualStyle="{x:Null}" />
Also, the Styleof focus might be:
此外,Style重点可能是:
<Style x:Key="MyItemFocusVisual" TargetType="{x:Type Control}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Control}">
<Border SnapsToDevicePixels="True" CornerRadius="0" BorderThickness="5" BorderBrush="#7B2F81" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Using:
使用:
<Grid Focusable="True" FocusVisualStyle="{StaticResource MyItemFocusVisual}" ... />
Output
Output


回答by Chris W.
If you just want to keep it from accepting focus via Tabbing just declare it on the object via IsTabStop="False"or you can edit the control Template for it and get rid of the Focus changes.
如果您只是想让它不通过 Tabbing 接受焦点,只需在对象上声明它,IsTabStop="False"或者您可以编辑它的控件模板并摆脱焦点更改。

