如何在 Silverlight 中设置文本框焦点? [英] How Do I Give a Textbox Focus in Silverlight?
问题描述
在我的 Silverlight 应用程序中,我似乎无法将焦点放在 TextBox 控件上.根据各种帖子的推荐,我已将 IsTabStop 属性设置为 True,并且我正在使用 TextBox.Focus().尽管 UserControl_Loaded 事件正在触发,但 TextBox 控件没有获得焦点.我在下面包含了我非常简单的代码.我错过了什么?谢谢.
In my Silverlight application, I can't seem to bring focus to a TextBox control. On the recommendation of various posts, I've set the IsTabStop property to True and I'm using TextBox.Focus(). Though the UserControl_Loaded event is firing, the TextBox control isn't getting focus. I've included my very simple code below. What am I missing? Thanks.
Page.xaml
<UserControl x:Class="TextboxFocusTest.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Loaded="UserControl_Loaded"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Width="150" VerticalAlignment="Center">
<TextBox x:Name="RegularTextBox" IsTabStop="True" />
</StackPanel>
</Grid>
</UserControl>
Page.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace PasswordTextboxTest
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
RegularTextBox.Focus();
}
}
}
推荐答案
我在 silverlight.net 上发现了这个,并且能够通过添加对 System.Windows.Browser.HtmlPage.Plugin 的调用让它为我工作.Focus() 在调用 RegularTextBox.Focus() 之前:
I found this on silverlight.net, and was able to get it to work for me by adding a call to System.Windows.Browser.HtmlPage.Plugin.Focus() prior to calling RegularTextBox.Focus():
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Browser.HtmlPage.Plugin.Focus();
RegularTextBox.Focus();
}
这篇关于如何在 Silverlight 中设置文本框焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!