在Silverlight中设置初始控件焦点

8
我正在寻找一种自动设置Silverlight UserControl初始焦点到特定控件的方法。我有一个登录页面,有一个用户名文本框,我希望用户在访问该页面时光标已经定位并等待在用户名文本框中,而不是让他们点击该框。
我尝试在UserControl的Loaded事件中调用.Focus,但没有成功。有人知道怎么做吗?
5个回答

16
public MainPage()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    HtmlPage.Plugin.Focus();
    MyTextBox.Focus();
}

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" />

1
这个解决方案似乎在IE中可行,但我无法让Silverlight控件在Firefox或Chrome中获得焦点。 - Nick Gotch
仍然无法在其他浏览器中使其工作,但我选择这个解决方案,因为大多数用户都会使用IE,其他用户只需单击Silverlight控件即可。谢谢! - Nick Gotch
不客气。我在使用Silverlight和FF时遇到了很多问题——调整大小和控件显示方面的问题。在某些情况下,我最终放弃了,在IE中有一个解决方案,在FF中有另一个解决方案。虽然这不是最好的方法,但也只能这样了。 - Scott Marlowe

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);
    }
}

1
如果你想要这段代码:

window.onload = function()

    {
        document.getElementById('Xaml1').focus();
    }

为了在所有浏览器中正常工作,您必须为id="Xaml1"的元素设置tabIndex。

抱歉我的英语不好 :)


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接