如何将两个文本框聚焦为Treeview的SelectedItemChanged事件被触发?

时间:2015-04-27 04:54:15

标签: c# wpf treeview focus

我正在尝试将两个文本框聚焦为用户在treeview中选择树视图项。我正在使用SlectedItemChanged事件来实现此目的。我需要同时关注两个文本框。

    delegate void voiDelegate();
    private void click(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        TreeViewItem t;
        t =(TreeViewItem) tvMain.SelectedItem;
        StackPanel s = (StackPanel)t.Header;
        List<TextBlock> l = new List<TextBlock>(3);
        foreach (TextBlock children in s.Children)
        {
            l.Add(children);
        }
        string ch = l[3].Text;
        string[] sp = ch.Split('-');
        int te = Convert.ToByte(sp[1]) - Convert.ToByte(sp[0]) + 1;
        PacketDisplay1.SelectionStart = PacketDisplay2.SelectionStart = Convert.ToByte(sp[0]);
        PacketDisplay1.SelectionLength= PacketDisplay2.SelectionLength = te;
        voiDelegate giveFocusDelegate = new voiDelegate(giveFocus);
        Dispatcher.BeginInvoke(giveFocusDelegate, new object[] { });

    }
    private void giveFocus()
    {
        PacketDisplay1.Focus();
        PacketDisplay2.Focus();
    }  

此处焦点仅在PacketDisplay2文本框中发生  如何在两个文本框中实现Focus?  感谢。

1 个答案:

答案 0 :(得分:0)

不可能同时关注2个文本框。 在调用PacketDisplay2.Focus()的那一刻,PacketDisplay1控件将失去焦点。 这是由windows设计的。

阅读以下链接:https://msdn.microsoft.com/en-us/library/aa969768%28v=vs.110%29.aspx

它清楚地说:Keyboard focus refers to the element that is currently receiving keyboard input. There can be only one element on the whole desktop that has keyboard focus.