Quantcast
Viewing latest article 2
Browse Latest Browse All 13

Answer by G.Dealmeida for WPF and initial focus

If you are like me, and you are using some frameworks that, somehow, mess up with the basic focus behaviors, and make all solutions above irrelevant, you can still do this :

1 - Note the element which get the focus (whatever it is!)

2 - Add this in your code behind xxx.xaml.cs

private bool _firstLoad;

3 - Add this on the element which get the first focus :

GotFocus="Element_GotFocus"

4 - Add the Element_GotFocus method in the code behind, and specify the WPF named element who need the first focus :

private void Element_GotFocus(object sender, RoutedEventArgs e){    if(_firstLoad)    {        this.MyElementWithFistFocus.Focus();        _firstLoad = false;    }}

5 - Manage the Loaded event

in XAML

Loaded="MyWindow_Loaded"

in xaml.cs

private void MyWindow_Loaded(object sender, RoutedEventArgs e){        _firstLoad = true;        this.Element_GotFocus(null, null);}

Hope this will help as a last resort solution


Viewing latest article 2
Browse Latest Browse All 13

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>