我有一个WPF窗口,在其中添加了一个按钮。我希望当应用程序启动时(即窗口被激活时),该按钮应该具有键盘焦点,并显示虚线边框。通常情况下,我们使用Tab键导航控件时会看到虚线边框。
我尝试了以下代码,但仍然觉得我漏掉了一些东西。
XAML
<Window x:Class="PropertyChangedTest.TestPropertyChangedWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Activated="Window_Activated">
<StackPanel Name="justPanel">
<Button Content="Hello" x:Name="Btn" Height="23" Width="52" Focusable="True" IsDefault="True" Click="Btn_Click"></Button>
</StackPanel>
</Window>
.cs file
private void Window_Activated(object sender, EventArgs e)
{
if (!bActivatedOnce)
{
bool bVisible = Btn.IsVisible;
UIElement elementWithFo = Keyboard.Focus(Btn) as UIElement;
bActivatedOnce = true;
}
}
获得了键盘焦点,但是它周围没有虚线边框。当我按下Alt键时,虚线边框会出现在按钮周围。