none
How can I get objects in DataTemplate when I use DataTemplateSelector and change the items in ItemSource dynamically? RRS feed

  • 질문

  • I have a ScatterView and insert items dynamically.

    ScatterView is binding to ItemSource with DataTemplateSelector.

    DataTemplateSelector selects a template in two templates - one has an image and the other has a thumnail and a movie and both of the templates have control buttons.

    I want to set the button.Content when a ScatterViewItem is created.

    I found I can get ContentPresenter using VisualTreeHelper like the code below, but the problem is that I couldn't get any ContentTemplate from ContentPresenter because I use DataTemplateSelector.

    How can I get my Surface Button object in this case?

    Can anybody help me?ㅠ_ㅠ

        public class ItemDataTemplateSelector : DataTemplateSelector
        {
            public DataTemplate ImageTemplate { get; set; }
            public DataTemplate StringTemplate { get; set; }

            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                FrameworkElement element = container as FrameworkElement;

                if (element != null && item != null && item is myItem)
                {
                    myItem scatterItem = item as myItem;

                    if (scatterItem.itemType == myItem.itemType_.IMAGE_ITEM)
                    {
                        return element.FindResource("myImageTemplate") as DataTemplate;
                    }
                    else if (scatterItem.itemType == myItem.itemType_.MEDIA_ITEM)
                    {
                        return element.FindResource("myMediaTemplate") as DataTemplate;
                    }
                }
                return null;
            }
        }


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

            private void scatterViewItem_OnLoaded(object sender, RoutedEventArgs e)

            {
                ScatterViewItem item = sender as ScatterViewItem;
                ContentPresenter content = FindContentPresenter(item);

        SurfaceButton editButton = content.ContentTemplate.FindName("EditModeButton", content) as SurfaceButton;

            }


    2012년 7월 27일 금요일 오전 6:40