设置初始控制的重点在Silverlight [英] Setting initial control focus in Silverlight

查看:141
本文介绍了设置初始控制的重点在Silverlight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方式来自动设置初始集中了Silverlight的用户控件到特定的控制。我有一个登录页面,输入用户名的文本框,我想有它,这样,一旦用户进入页面的光标已经定位,并在用户名文本框等,而不必让他们单击框。

I'm looking for a way to automatically set the initial focus on a Silverlight UserControl to a specific control. I have a login page with a user name textbox and I'd like to have it so that as soon as the user goes to the page their cursor is already positioned and waiting in the username textbox instead of having to make them click the box.

我试图在用户控件的加载事件,但没有成功调用.Focus。任何人都知道如何做到这一点?

I tried calling .Focus in the UserControl's Loaded event but with no success. Anyone know how to do this?

推荐答案

我刮起了快速SL3应用程序,它的的很难​​有最初的重点转到用户控件更不用说控制范围内Silverlight控件。

I whipped up a quick SL3 app and it is difficult to have the initial focus go to the UserControl let alone to a control within the Silverlight control.

不过,看看该解决方案为您解决了这个问题。你将不得不使用一些JavaScript。

However, see if this solution solves this issue for you. You'll have to use a little JavaScript.

这里的code,以供参考:

Here's the code for reference:

<%@ 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控件焦点,您可以使用类似进一步将焦点设置到一个特定的控制:

Once your SL control has focus, you can further set the focus to a specific control using something like:

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定义为:

where uxLogin is defined in XAML as:

<TextBox x:Name="uxLogin" Height="25" Width="100" />

这篇关于设置初始控制的重点在Silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆