How to set autofocus only in xaml?

后端 未结 5 1105
耶瑟儿~
耶瑟儿~ 2020-12-17 08:02

Is it possible, to set the autofocus to the textbox in my xaml file?



        
相关标签:
5条回答
  • 2020-12-17 08:41

    I usually resolve this with C#

    textBox1.Focus();
    

    I think it is the best way how to do this.

    0 讨论(0)
  • 2020-12-17 08:48

    I think binding is an overkill, Reference is more lightweight:

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            FocusManager.FocusedElement="{x:Reference textBox1}">
        <StackPanel>
            <TextBox x:Name="textBox1" />
        </StackPanel>
    </Window>
    
    0 讨论(0)
  • 2020-12-17 08:51
    <TextBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" />
    
    0 讨论(0)
  • 2020-12-17 09:01

    try somethind like this

    <Window x:Class="WpfApplication18.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="500" Width="525" FocusManager.FocusedElement="textcontrol">
        <Grid>
           <TextBox Name="textcontrol" />
        </Grid>
    </Window>
    
    0 讨论(0)
  • 2020-12-17 09:02

    yes you can use FocusManager.FocusedElement attached property.

    FocusManager.FocusedElement="{Binding ElementName=textBox1}"
    
    0 讨论(0)
提交回复
热议问题