The simple usage is as follows:
In the parent class container, use the attached property focusmanager.focusedelement to bind the control that needs to be forced to focus, using the following:
<grid Focusmanager. focusedelement= "{Binding elementname=btn}" >
<button x:name= "btn" content= "1234"/>
</Grid>
It is important to note that when a control uses a style or a template to override the structure of a control, this setting may be invalid, and it needs to go into the template to set
You can view examples:
1 <Windowx:class= "Focusmanagerdemo.mainwindow"2 xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"3 xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"4 Title= "MainWindow"Height= " the"Width= "525">5 <Grid>6 <!--when you need to test the outer button, you can remove the comment, noting that only one control in the same moment can get focus <grid focusmanager.focusedelement= "{Binding elementname=btn}" > -7 <Buttonx:name= "BTN"Content= "1234"/>8 <Grid>9 <TextBoxx:name= "txt"Text= "ABC"Margin= "106,73,97,145">Ten <Textbox.style> One <StyleTargetType= "TextBox"> A <Setter Property= "Template"> - <Setter.value> - <ControlTemplateTargetType= "TextBox"> the <Gridfocusmanager.focusedelement="{Binding elementname=btn123}"> - <Buttonx:name= "btn123"Content= "123"Margin= "Ten"/> - </Grid> - </ControlTemplate> + </Setter.value> - </Setter> + </Style> A </Textbox.style> at </TextBox> - </Grid> - </Grid> - </Window>
View Code
WPF settings control gets focus Focusmanager