我正在寻找一种方法来自动将Silverlight UserControl的初始焦点设置为特定控件。我有一个带有用户名文本框的登录页面,我想拥有它,这样一旦用户进入页面,他们的光标已经定位并在用户名文本框中等待而不必让他们单击该框。
我尝试在UserControl的Loaded事件中调用.Focus,但没有成功。有谁知道怎么做?
答案 0 :(得分:16)
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.Plugin.Focus();
MyTextBox.Focus();
}
答案 1 :(得分:5)
我掀起了一个快速的SL3应用程序 很难让初始焦点转到UserControl,更不用说转到Silverlight控件中的控件了。
但是,请查看this solution是否为您解决了此问题。你必须使用一点JavaScript。
以下是供参考的代码:
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="height:100%;">
<head runat="server">
<title>Test Page For TextFocusTest</title>
<script type="text/javascript">
window.onload = function()
{
document.getElementById('Xaml1').focus();
}
</script>
</head>
<body style="height:100%;margin:0;">
<form id="form1" runat="server" style="height:100%;">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="height:100%;">
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TextFocusTest.xap" Version="2.0" Width="100%" Height="100%" />
</div>
</form>
</body>
</html>
一旦您的SL控件具有焦点,您可以使用以下内容进一步将焦点设置为特定控件:
namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage ()
{
InitializeComponent ();
this.GotFocus += new RoutedEventHandler (MainPage_GotFocus);
}
void MainPage_GotFocus (object sender, RoutedEventArgs e)
{
uxLogin.Focus ();
}
}
}
其中uxLogin在XAML中定义为:
<TextBox x:Name="uxLogin" Height="25" Width="100" />
答案 2 :(得分:3)
如果你想以PRISM或MVVM的方式(摆脱代码隐藏代码),你可以实现一个行为。在我的情况下,我将视图集中在用户名字段中,如果它是空的,并在密码中,如果它已设置(因此是2个参数)。
我的实施:
public static class ControlTextFocusBehavior
{
public static readonly DependencyProperty FocusParameterProperty = DependencyProperty.RegisterAttached(
"FocusParameter",
typeof(string),
typeof(ControlTextFocusBehavior),
new PropertyMetadata(OnSetFocusParameterCallBack));
public static readonly DependencyProperty IsEmptyFocusedProperty = DependencyProperty.RegisterAttached(
"IsEmptyFocused",
typeof(bool),
typeof(ControlTextFocusBehavior),
new PropertyMetadata(true));
private static void OnSetFocusParameterCallBack(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
Control control = dependencyObject as Control;
if (control != null)
{
control.Loaded += new RoutedEventHandler(control_Loaded);
}
}
private static void control_Loaded(object sender, RoutedEventArgs e)
{
Control control = sender as Control;
control.Loaded -= new RoutedEventHandler(control_Loaded);
DependencyObject dependencyObject = sender as DependencyObject;
if (dependencyObject != null)
{
bool isEmptyFocused = GetIsEmptyFocused(dependencyObject);
bool isNullOrEmpty = string.IsNullOrEmpty(GetFocusParameter(dependencyObject));
if ((isEmptyFocused && isNullOrEmpty) ||
(!isEmptyFocused && !isNullOrEmpty))
{
HtmlPage.Plugin.Focus();
control.Focus();
}
}
}
public static void SetFocusParameter(DependencyObject dependencyObject, string parameter)
{
dependencyObject.SetValue(FocusParameterProperty, parameter);
}
public static string GetFocusParameter(DependencyObject dependencyObject)
{
return dependencyObject.GetValue(FocusParameterProperty) as string;
}
public static void SetIsEmptyFocused(DependencyObject dependencyObject, bool parameter)
{
dependencyObject.SetValue(IsEmptyFocusedProperty, parameter);
}
public static bool GetIsEmptyFocused(DependencyObject dependencyObject)
{
return (bool)dependencyObject.GetValue(IsEmptyFocusedProperty);
}
}
答案 3 :(得分:1)
如果您需要此代码:
window.onload = function()
{ document.getElementById('Xaml1').focus(); }
在所有浏览器中工作,必须为id =“Xaml1”的元素设置tabIndex。
对不起我的英文:)
答案 4 :(得分:0)
我刚做了一个快速测试,看起来这也解决了浏览器中的问题!
How to set focus on TextBox in Silverlight 4 out-of-browser popup