C# 在 WPF 中将焦点设置在文本框上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1345391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set focus on textbox in WPF
提问by priyanka.sarkar
How to set the focus on an TextBox
element in WPF
如何将焦点设置TextBox
在 WPF 中的元素上
I have this code:
我有这个代码:
txtCompanyID.Focusable = true;
txtCompanyID.Focus();
...but it is not working.
...但它不起作用。
Any idea?
任何的想法?
采纳答案by usefulBee
In XAML:
在 XAML 中:
<StackPanel FocusManager.FocusedElement="{Binding ElementName=Box}">
<TextBox Name="Box" />
</StackPanel>
回答by Arsen Mkrtchyan
try FocusManager.SetFocusedElement
试试FocusManager.SetFocusedElement
FocusManager.SetFocusedElement(parentElement, txtCompanyID)
回答by Svetlozar Angelov
txtCompanyID.Focusable = true;
Keyboard.Focus(txtCompanyID);
msdn:
msdn:
There can be only one element on the whole desktop that has keyboard focus. In WPF, the element that has keyboard focus will have IsKeyboardFocused set to true.
整个桌面上只能有一个元素具有键盘焦点。在 WPF 中,具有键盘焦点的元素会将 IsKeyboardFocused 设置为 true。
You could break after the setting line and check the value of IsKeyboardFocused
property. Also check if you really reach that line or maybe you set some other element to get focus after that.
您可以在设置行之后中断并检查IsKeyboardFocused
属性值。还要检查您是否真的到达了那条线,或者您是否设置了一些其他元素以在此之后获得焦点。
回答by NazOok
Try this : MyTextBox.Focus ( );
尝试这个 : MyTextBox.Focus ( );
回答by Anya Hope
None of this worked for me as I was using a grid rather than a StackPanel.
这些都不适合我,因为我使用的是网格而不是 StackPanel。
I finally found this example: http://spin.atomicobject.com/2013/03/06/xaml-wpf-textbox-focus/
我终于找到了这个例子:http: //spin.atomicobject.com/2013/03/06/xaml-wpf-textbox-focus/
and modified it to this:
并将其修改为:
In the 'Resources' section:
在“资源”部分:
<Style x:Key="FocusTextBox" TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=textBoxName, Path=IsVisible}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=textBoxName}"/>
</DataTrigger>
</Style.Triggers>
</Style>
In my grid definition:
在我的网格定义中:
<Grid Style="{StaticResource FocusTextBox}" />
回答by Peter Huber
Nobody explained so far why the code in the question doesn't work. My guess is that the code was placed in the constructor of the Window. But at this time it's too early to set the focus. It has to be done once the Window is ready for interaction. The best place for the code is the Loaded event:
到目前为止,没有人解释为什么问题中的代码不起作用。我的猜测是代码被放置在 Window 的构造函数中。但此时定下焦点还为时过早。一旦窗口准备好进行交互,就必须完成它。代码最好的地方是 Loaded 事件:
public KonsoleWindow() {
public TestWindow() {
InitializeComponent();
Loaded += TestWindow_Loaded;
}
private void TestWindow_Loaded(object sender, RoutedEventArgs e) {
txtCompanyID.Focus();
}
}
回答by Alex Russkov
Another possible solution is to use FocusBehaviorprovided by free DevExpress MVVM Framework:
另一种可能的解决方案是使用免费的 DevExpress MVVM 框架提供的FocusBehavior:
<TextBox Text="This control is focused on startup">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:FocusBehavior/>
</dxmvvm:Interaction.Behaviors>
</TextBox>
It allows you to focus a control when it's loaded, when a certain event is raised or a property is changed.
它允许您在加载控件、引发特定事件或更改属性时聚焦控件。
回答by EasyJoin Dev
In case you haven't found the solution on the other answers, that's how I solved the issue.
如果您还没有在其他答案中找到解决方案,那么我就是这样解决问题的。
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
TEXTBOX_OBJECT.Focus();
}), System.Windows.Threading.DispatcherPriority.Render);
From what I understand the other solutions may not work because the call to Focus()
is invoked before the application has rendered the other components.
据我了解,其他解决方案可能不起作用,因为Focus()
在应用程序呈现其他组件之前调用了 。