I have a WPF Window in which I have added a button. I want the button should have the keyboard focus with dotted border around it when the application starts up (basically when the window is activated). Normally we see the dotted border when we navigate through controls using Tab key.
I tried the following code but still i think I am missing something.
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;
}
}
The button has the keyboard focus but it doesnt have the dotted border around it. When i press Alt key the dotted border appears around the button.