如何在以编程方式创建的文本框中设置焦点?

时间:2010-02-12 15:51:05

标签: c# wpf textbox focus

当窗口出现时,我需要对以下代码做什么才能使光标在第二个文本框中闪烁

XAML:

<Window x:Class="TestFocksdfj.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left" Margin="10">
        <ContentControl x:Name="FormArea"/>
    </StackPanel>
</Window>

代码背后:

using System.Windows;
using System.Windows.Controls;

namespace TestFocksdfj
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            StackPanel sp = new StackPanel();

            for (int i = 0; i < 3; i++)
            {
                TextBox tb = new TextBox();
                tb.Width = 200;
                tb.Margin = new Thickness { Bottom = 3 };
                if (i == 1)
                    tb.Focus();
                sp.Children.Add(tb);
            }

            FormArea.Content = sp;
        }
    }
}

3 个答案:

答案 0 :(得分:4)

在您致电FormArea.Content = sp;后,您可以致电sp.Children[1].Focus();以获得第二个文本框焦点。

像这样:

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            StackPanel sp = new StackPanel();

            for (int i = 0; i < 3; i++)
            {

                TextBox tb = new TextBox();
                tb.Width = 200;
                tb.Margin = new Thickness { Bottom = 3 };
                sp.Children.Add(tb);
            }
            FormArea.Content = sp;
            sp.Children[1].Focus();
        }
    }

答案 1 :(得分:1)

刚刚找到http://apocryph.org/2006/09/10/wtf_is_wrong_with_wpf_focus的解决方案,在我的情况下运行良好,但是没有更标准的方法在没有这样的黑客的情况下在WPF中执行此操作?

using System.Windows;
using System.Windows.Controls;
using System.Threading;
using System;
using System.Windows.Input;

namespace TestFocksdfj
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            StackPanel sp = new StackPanel();

            for (int i = 0; i < 3; i++)
            {
                TextBox tb = new TextBox();
                tb.Width = 200;
                tb.Margin = new Thickness { Bottom = 3 };
                if (i == 2)
                {
                    FocusHelper.Focus(tb);
                }
                sp.Children.Add(tb);
            }

            FormArea.Content = sp;
        }
    }

    //thanks to: http://apocryph.org/2006/09/10/wtf_is_wrong_with_wpf_focus/
    static class FocusHelper
    {
        private delegate void MethodInvoker();

        public static void Focus(UIElement element)
        {
            ThreadPool.QueueUserWorkItem(delegate(Object foo)
            {
                UIElement elem = (UIElement)foo;
                elem.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (MethodInvoker)delegate()
                    {
                        elem.Focus();
                        Keyboard.Focus(elem);
                    });
            }, element);
        }
    }

}

答案 2 :(得分:0)

您会认为tb.focus将是所有必需的。您可以尝试将第二个文本框的tabindex设置为0,然后尝试tb.focus。另一种选择是JavaScript ......

private void Set_Focus(string controlname)
{
string strScript;

strScript = "<script language=javascript> document.all('" + controlname + "').focus() </script>";
RegisterStartupScript("focus", strScript);
}