none
wpf 在listBox后台怎么取Template里面元素的值 RRS feed

  • 问题

  • 我现在有个ListBox,在Template里面有个TextBox,我在ListBox的Style里面写了KeyDown事件,如:<EventSetter Event="KeyDown" Handler="ListBoxItem_KeyDown"/>,在后台控制,如果是按下F2键,就进入编辑状态,则显示Template里面的TextBox,隐藏原来的TextBlock,他们都是绑定VM里面的Name属性,但是进入编辑状态时,我想让TextBox获取焦点,但是我在ListBoxItem_KeyDown事件里面,不知道怎么去获取ListBoxITem的Template里面的TextBox对象,各位大侠能指点一下吗,非常感谢。。。。。

    程序人生

    2013年5月7日 13:28

答案

  • 你好,

    如果要检索由某个 ListBoxItem DataTemplate 生成的 TextBox 元素,您需要获得 ListBoxItem,在该 ListBoxItem 内查找 ContentPresenter,然后对在该 ContentPresenter 上设置的 DataTemplate 调用 FindName

    下面的示例演示如何执行这些步骤

           private void ListBoxItem_KeyDown_1(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.F2)
                {
                    ListBoxItem myListBoxItem = (ListBoxItem)(listbox.ItemContainerGenerator.ContainerFromItem(listbox.Items.CurrentItem));
                    ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
    
                   DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
                   TextBox myTextBox = (TextBox)myDataTemplate.FindName("textbox", myContentPresenter);
    
                   MessageBox.Show("The text of the TextBlock of the selected list item:" + myTextBox.Text);
    
                }
            }
    
            private childItem FindVisualChild<childItem>(DependencyObject obj)
        where childItem : DependencyObject
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is childItem)
                        return (childItem)child;
                    else
                    {
                        childItem childOfChild = FindVisualChild<childItem>(child);
                        if (childOfChild != null)
                            return childOfChild;
                    }
                }
                return null;
            }
    

    请参考:http://msdn.microsoft.com/zh-cn/library/bb613579.aspx


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年5月10日 14:27
    版主

全部回复

  • 你好,

    可以 把你的代码贴出来吗?

    2013年5月10日 10:11
  • 你好,

    如果要检索由某个 ListBoxItem DataTemplate 生成的 TextBox 元素,您需要获得 ListBoxItem,在该 ListBoxItem 内查找 ContentPresenter,然后对在该 ContentPresenter 上设置的 DataTemplate 调用 FindName

    下面的示例演示如何执行这些步骤

           private void ListBoxItem_KeyDown_1(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.F2)
                {
                    ListBoxItem myListBoxItem = (ListBoxItem)(listbox.ItemContainerGenerator.ContainerFromItem(listbox.Items.CurrentItem));
                    ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
    
                   DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
                   TextBox myTextBox = (TextBox)myDataTemplate.FindName("textbox", myContentPresenter);
    
                   MessageBox.Show("The text of the TextBlock of the selected list item:" + myTextBox.Text);
    
                }
            }
    
            private childItem FindVisualChild<childItem>(DependencyObject obj)
        where childItem : DependencyObject
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child != null && child is childItem)
                        return (childItem)child;
                    else
                    {
                        childItem childOfChild = FindVisualChild<childItem>(child);
                        if (childOfChild != null)
                            return childOfChild;
                    }
                }
                return null;
            }
    

    请参考:http://msdn.microsoft.com/zh-cn/library/bb613579.aspx


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年5月10日 14:27
    版主