我有一个WPF UserControl,Focusable ="True".它是窗口中唯一可聚焦的控件.
每当用户按Tab或Alt(特别是当他们按Alt + Tab键到另一个应用程序)时,我的UserControl会获得一个虚线边框,即焦点矩形.然后焦点矩形停留在那里直到窗口关闭.
如何防止我的UserControl显示此焦点矩形?
事实证明,我的UserControl实际上并没有显示焦点矩形.My Focusable UserControl包含另一个UserControl,而UserControl又包含ItemsControl,而ItemsControl则显示焦点矩形.
当我将FocusVisualStyle ="{x:Null}"添加到ItemsControl时,焦点矩形消失了.
在WPF中,我创建了一个自定义按钮模板,如下所示:
<ToggleButton x:Name="button" HorizontalAlignment="Left" Margin="0,0,0,0" >
<ToggleButton.Template>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid Background="#01FFFFFF" Width="62" Height="53">
<Border VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="#01FFFFFF" IsHitTestVisible="True"/>
<Canvas Width="26" Height="32" x:Name="background" IsHitTestVisible="True" >
<!-- Omitted for brevity--->
</Canvas>
</Grid>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
Run Code Online (Sandbox Code Playgroud)
这段代码的结果是一个简单的Off按钮:
现在,当此按钮变为鼠标焦点,然后屏幕失去焦点时,按钮会在其周围出现虚线边框,如下所示:

我的问题很简单:如何摆脱这种边界?我尝试过使用这些FocusVisualStyle属性,但无法让它工作.
我有一个UserControl基本上这样的包装ListBox-
<ListBox x:Name="lb" ItemsSource="{Binding ElementName=UC,Path=Pages}"
Background="{Binding ElementName=UC,Path=Background}"
BorderBrush="Transparent"
ScrollViewer.CanContentScroll="False"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="{Binding ElementName=UC,Path=ActualWidth}">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="20"/>
<ColumnDefinition/>
<ColumnDefinition MinWidth="20"/>
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="1" Content="{Binding}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我需要设置FocusVisualStyle来{x:Null}隐藏这个功能,但无论在哪里应用它,我仍然得到默认的蓝色选择颜色.我已经尝试在ListBox,StackPanel和Grid上设置它,但无济于事.
任何帮助都会很棒.谢谢.