如何在 Silverlight 4 浏览器外弹出窗口中对 TextBox 设置焦点 [英] How to set focus on TextBox in Silverlight 4 out-of-browser popup

查看:9
本文介绍了如何在 Silverlight 4 浏览器外弹出窗口中对 TextBox 设置焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的ChildWindow Silverlight 4(测试版)中的弹出窗口.

I have a simple ChildWindow popup in Silverlight 4 (beta).

重要提示:这是一个浏览器外应用程序.

我想在窗口打开时自动将焦点设置在 TextBox 控件上.

i want to auto set focus on a TextBox control when the window opens.

我尝试了几件事:

以下代码似乎没有做任何事情.我认为在加载"之后控件还没有准备好聚焦.

The following code doesn't seem to do anything. I don't think the control is ready to be focussed after 'Loading'.

    private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
    {
          textBox1.Focus();
    }

这行得通,但很笨拙.

    private void ChildWindow_GotFocus(object sender, RoutedEventArgs e)
    {
          if (_firstTime == true) {
              textBox1.Focus();
             _firstTime = false;
          }
    }

没有更好的方法吗?我总是不得不在 WinForms 中做这样可怕的事情,但希望不再需要这样做.

Isn't there a better way? I always had to do horrible things like this in WinForms but was hoping not to have to anymore.

注意:这个类似问题是针对仅在浏览器中.它建议调用 System.Windows.Browser.HtmlPage.Plugin.Focus(); 这不起作用,实际上在浏览器外的 Silverlight 4 beta 上运行时会出错.

Note: This similar question is for in browser only. It suggests calling System.Windows.Browser.HtmlPage.Plugin.Focus(); which doesn't work and in fact gives an error when running on Silverlight 4 beta out-of-browser.

推荐答案

我在 SilverLight 4 (OOB) 中遇到了同样的问题,我注意到选项卡序列会将焦点设置到我看不到的控件上.似乎正在发生的事情是焦点被设置到您的控件(选项卡序列中的第一个),然后由于某种原因,焦点移动到 ContentControl(名称 =内容"),它(我认为)是子窗口.

I was having the same problem in SilverLight 4 (OOB) and I noticed that the tab sequence would set focus to a control that i could not see. What appears to be happening is the focus is being set to your control (first one in the tab sequence) and then for some reason the focus moves to the ContentControl (name ="content"), which (i think) is the parent of the child window.

ContentControl 默认具有 IsTabStop=true.看....为什么我希望 IsTabStop 在内容控制?要为应用中的所有 ContentControl 设置 ContentControl.IsTabStop = false,请将其添加到 style.xaml 中.

ContentControl by default has IsTabStop=true. see.... Why would I want IsTabStop set to true on a ContentControl? To set the ContentControl.IsTabStop = false for all ContentControls in your app, add this to your styles.xaml.

   <Style  TargetType="ContentControl" >
         <Setter Property="IsTabStop" Value="false"/>
    </Style>

同样的问题发生在 MainPage 上的 Tab 序列上.这种风格也会解决这个问题.

The same issue happens with the tab sequence on the MainPage. This style will also fix this.

这篇关于如何在 Silverlight 4 浏览器外弹出窗口中对 TextBox 设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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