【问题标题】:The easiest way to set focus on single textBox upon onload在加载时将焦点设置在单个文本框上的最简单方法
【发布时间】:2014-01-17 17:39:51
【问题描述】:

基于此链接:Set the focus on a textbox in xaml wpfFocus 可以通过在StackPanel 处设置FocusManager 并指定元素名称来设置。我确实试过了,它有效。我想知道如果我的XAMLStackPanel 中只有一个文本框是不必要的怎么办?

在我只有一个文本框的情况下,还有其他简单的方法来设置焦点吗?

我也尝试了上面链接中的第二个答案,但不起作用:

<TextBox Text="{Binding MyText}" 
    FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    我过去通过将焦点放在标题中来做到这一点 - 在这种情况下是用户控件,但对于窗口应该是相同的)

    <UserControl .....
    FocusManager.FocusedElement="{Binding ElementName=TextBoxName}">
    

    【讨论】:

      【解决方案2】:
      <TextBox Text="{Binding MyText}" 
             FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource AncestorType=TextBox}}"/>
      

      <TextBox x:Name="MyTBox" Text="{Binding MyText}" 
             FocusManager.FocusedElement="{Binding ElementName=MyTBox}"/>
      

      这将适用于您的情况。

      【讨论】:

        猜你喜欢
        • 2011-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-02
        • 1970-01-01
        • 1970-01-01
        • 2016-05-23
        相关资源
        最近更新 更多