none
我的主界面上有个ListBox双击Item的时候弹出窗体,关闭弹出的窗体后,咋样继续让主界面的ListBox获取焦点 RRS feed

  • 问题

  • 我的主界面上有个ListBox双击Item的时候弹出窗体,关闭弹出的窗体后,咋样继续让主界面的ListBox获取焦点

    我是这样写的

            int selectIndex = 0;
            private void ListViewContent_TouchDown(object sender, TouchEventArgs e)
            {
                //MessageBox.Show("Time:  " + e.Timestamp);
                ListBox ls = sender as ListBox;
                selectIndex = ls.SelectedIndex;
                Device lsItem = ls.SelectedItem as Device;
                if (lsItem == null) return;
                    //1、实例化弹出窗
                    DevDetaillControl ddc = new DevDetaillControl(lsItem);
                    ddc.Closed += new EventHandler(ddc_Closed);
                    ddc.ShowDialog();
            }

            void ddc_Closed(object sender, EventArgs e)
            {
                this.ListViewContent.Focus();
                this.ListViewContent.SelectedIndex = selectIndex;
            }

    这样,关闭弹出窗体的时候能将ListViewContent 的Item选中,但是ListViewContent这个控件就是无法获取焦点,我再次点击的时候才能获取。

    2014年8月12日 2:49

答案

  • 你好,

    据我所知,我们只可以使的ListBoxItem获取到焦点,对于ListBox设置焦点是不起作用的。

    我测试了你的代码,是可以正常工作的,我想知道你要让ListBox获取焦点的目的是什么呢,如果你是想触发焦点事件,那可以对ListBoxItem进行焦点事件的设置。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月13日 10:37
    版主
  •         void ddc_Closed(object sender, EventArgs e)
            {
                //this.ListViewContent.Focus();
                //this.ListViewContent.SelectedIndex = 3;
                ListBoxItem item = ListViewContent.ItemContainerGenerator.ContainerFromIndex(selectIndex-1) as ListBoxItem;
                if (item != null)
                    //item.IsSelected = true;
                    item.Focus();
                //Keyboard.Focus(item);
                getFoucs();
            }

            void getFoucs() {
                foreach (System.Windows.UIElement element in MainGrid.Children)
                {
                    if (element.IsFocused == true)
                    {
                        MessageBox.Show("IsFocused: " + element);
                    }
                }
            }

    我是这样实现的,关闭弹出窗体后,执行getFoucs() , 发现主界面上没有控件获取焦点

    你好,

    因为你是对 ListBoxItem 进行聚焦,你所以你应该检测 ListBoxItem 的 IsFocused 属性:

    void getFoucs()
    {
                foreach (var item in this.ListViewContent.Items)
                {
                    ListBoxItem container = ListViewContent.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
                    if (container.IsFocused == true)
                    {
                        MessageBox.Show("IsFocused: " + container);
                    }
                }
    }

    截图:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月15日 7:04
    版主
  • 请问 咋样让主界面的ListBox获取焦点呢?

    你好,

    如我第一个回复中说的,ListBox中只可以对Item设置焦点,对ListBox本身设置焦点是不起作用的。



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 weiss_2009 2014年8月19日 9:13
    2014年8月19日 8:21
    版主

全部回复

  • 你好,

    据我所知,我们只可以使的ListBoxItem获取到焦点,对于ListBox设置焦点是不起作用的。

    我测试了你的代码,是可以正常工作的,我想知道你要让ListBox获取焦点的目的是什么呢,如果你是想触发焦点事件,那可以对ListBoxItem进行焦点事件的设置。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月13日 10:37
    版主
  •         void ddc_Closed(object sender, EventArgs e)
            {
                //this.ListViewContent.Focus();
                //this.ListViewContent.SelectedIndex = 3;
                ListBoxItem item = ListViewContent.ItemContainerGenerator.ContainerFromIndex(selectIndex-1) as ListBoxItem;
                if (item != null)
                    //item.IsSelected = true;
                    item.Focus();
                //Keyboard.Focus(item);
                getFoucs();
            }

            void getFoucs() {
                foreach (System.Windows.UIElement element in MainGrid.Children)
                {
                    if (element.IsFocused == true)
                    {
                        MessageBox.Show("IsFocused: " + element);
                    }
                }
            }

    我是这样实现的,关闭弹出窗体后,执行getFoucs() , 发现主界面上没有控件获取焦点
    2014年8月14日 1:54
  •         void ddc_Closed(object sender, EventArgs e)
            {
                //this.ListViewContent.Focus();
                //this.ListViewContent.SelectedIndex = 3;
                ListBoxItem item = ListViewContent.ItemContainerGenerator.ContainerFromIndex(selectIndex-1) as ListBoxItem;
                if (item != null)
                    //item.IsSelected = true;
                    item.Focus();
                //Keyboard.Focus(item);
                getFoucs();
            }

            void getFoucs() {
                foreach (System.Windows.UIElement element in MainGrid.Children)
                {
                    if (element.IsFocused == true)
                    {
                        MessageBox.Show("IsFocused: " + element);
                    }
                }
            }

    我是这样实现的,关闭弹出窗体后,执行getFoucs() , 发现主界面上没有控件获取焦点

    你好,

    因为你是对 ListBoxItem 进行聚焦,你所以你应该检测 ListBoxItem 的 IsFocused 属性:

    void getFoucs()
    {
                foreach (var item in this.ListViewContent.Items)
                {
                    ListBoxItem container = ListViewContent.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
                    if (container.IsFocused == true)
                    {
                        MessageBox.Show("IsFocused: " + container);
                    }
                }
    }

    截图:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月15日 7:04
    版主
  • 请问 咋样让主界面的ListBox获取焦点呢?
    2014年8月15日 7:51
  • 请问 咋样让主界面的ListBox获取焦点呢?

    你好,

    如我第一个回复中说的,ListBox中只可以对Item设置焦点,对ListBox本身设置焦点是不起作用的。



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 weiss_2009 2014年8月19日 9:13
    2014年8月19日 8:21
    版主
  • 谢谢   已经解决了!
    2014年8月19日 9:13
  • 谢谢   已经解决了!

    不客气~

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月19日 9:16
    版主