简体   繁体   English

触发Treeview的SelectedItemChanged事件时,如何聚焦两个文本框?

[英]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. 当用户在treeview中选择treeview项目时,我试图集中两个文本框。 I am using a SlectedItemChanged event to achieve this. 我正在使用SlectedItemChanged事件来实现此目的。 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. 在这里,焦点仅在PacketDisplay2文本框中发生。
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. 调用PacketDisplay2.Focus()时,PacketDisplay1控件将失去焦点。 This is by design of windows. 这是Windows设计的。

Read following link : https://msdn.microsoft.com/en-us/library/aa969768%28v=vs.110%29.aspx 阅读以下链接: https : //msdn.microsoft.com/zh-cn/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. 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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