简体   繁体   中英

How to Focus Two Text Boxes as SelectedItemChanged event of Treeview is triggerd?

I am trying to focus the two text boxes as the user selects the treeview item in treeview. I am using a SlectedItemChanged event to achieve this. I need to focus both the text boxes at the same time.

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

Here focus is happening only in PacketDisplay2 text box.
how can I achieve Focus in Both text boxes? thanks.

It is impossible to give focus to 2 textboxes at the same time. The moment you call PacketDisplay2.Focus(), the PacketDisplay1 control will lose focus. This is by design of windows.

Read following link : https://msdn.microsoft.com/en-us/library/aa969768%28v=vs.110%29.aspx

It says clearly : 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM