none
如何取得ListView 中,某一条的template 信息 RRS feed

  • 问题

  • 是这样的过程:

    1. 定义了一个 Listview 的每个Item的MessageListImageTemplate,格式包括:Rectangle、TextBlock、Image

    2.设置 ListView 的 template 为MessageListImageTemplate

     

    ItemTemplate

    ="{StaticResourceMessageListImageTemplate}"

    3. 我希望在ListView 的

    ItemListView_SelectionChanged 函数中,得到选中行的template。然后template->Rectangle->Visibility = Collapsed!

    这个过程该如何实现呢??

    2012年9月20日 13:54

答案

  •         private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                ListBoxItem ListBoxItemObj = (ListBoxItem)listBox1.ItemContainerGenerator.ContainerFromItem(listBox1.Items[1]);
                TextBlock textblock = (TextBlock)FindVisualChildByName(ListBoxItemObj, "textBlock");
                textblock.Foreground = new SolidColorBrush(Colors.Red);
            }


            public FrameworkElement FindVisualChildByName(DependencyObject obj, string name)
            {
                FrameworkElement ret = null;
                int numChildren = VisualTreeHelper.GetChildrenCount(obj);
                for (int i = 0; i < numChildren; i++)
                {
                    var objChild = VisualTreeHelper.GetChild(obj, i);
                    var child = objChild as FrameworkElement;
                    if (child != null && child.Name == name)
                    {
                        return child;
                    }

                    ret = FindVisualChildByName(objChild, name);
                    if (ret != null)
                        break;
                }
                return ret;
            }


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年9月21日 2:58

全部回复

  • Template中的element或是control是不能通过name attribute去引用的,一般都是尽量在template里面用trigger去定义这些操作。

    你可以试试用VisualTreeHelper.GetChild去递归查找rectangle(现在没有WIN8环境可以试,我也不确定好用不)


    Hard work, hard play!

    2012年9月20日 16:06
  • 对template 里边的 trigger & VisualTreeHelper.GetChild 不大明白!

    有没有Win8 的例子?

    谢谢啊!

    2012年9月21日 2:36
  •         private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                ListBoxItem ListBoxItemObj = (ListBoxItem)listBox1.ItemContainerGenerator.ContainerFromItem(listBox1.Items[1]);
                TextBlock textblock = (TextBlock)FindVisualChildByName(ListBoxItemObj, "textBlock");
                textblock.Foreground = new SolidColorBrush(Colors.Red);
            }


            public FrameworkElement FindVisualChildByName(DependencyObject obj, string name)
            {
                FrameworkElement ret = null;
                int numChildren = VisualTreeHelper.GetChildrenCount(obj);
                for (int i = 0; i < numChildren; i++)
                {
                    var objChild = VisualTreeHelper.GetChild(obj, i);
                    var child = objChild as FrameworkElement;
                    if (child != null && child.Name == name)
                    {
                        return child;
                    }

                    ret = FindVisualChildByName(objChild, name);
                    if (ret != null)
                        break;
                }
                return ret;
            }


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年9月21日 2:58