locked
How to access a named element in a ListViewItem generated by DataTemplate?

    Question

  • I have a ListView and want to access a named element in the DataTemplate. Normally, there's many methods to do this (like this article), but my problem is I want to access the element when the ListViewItem's DataTemplate is applied(since I want to attach some EventHandler to this element). I don't know in which event or overridable method I can do this. I have tried these two method, but won't work:

    1. Extand ListView and override it's PrepareContainerForItemOverride() method

        public  class ListViewEx : ListView
        {
    
            protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
            {
                base.PrepareContainerForItemOverride(element, item);
    
                ListViewItem lvi = (ListViewItem)element;
    
                //lvi is empty
    
             }
    }

    2. Extand ListViewItem and ListView:

        public class ListViewItemEx : ListViewItem
        {
            protected override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                //FrameworkElement a = this.GetTemplateChild("MyElement"); //a == null
    
                ListViewItemPresenter presenter = this.FindFirstChild<ListViewItemPresenter>();
                DataTemplate template = presenter.ContentTemplate; //template==null
    FrameworkElement a = template.FindChild("MyElement");
            }
        }
        [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(ListViewItemEx))]
        public  class ListViewEx : ListView
        {
            protected override DependencyObject GetContainerForItemOverride()
            {
                return new ListViewItemEx();
            }
    }

    Is there anything wrong with my code? Or these two method just won't work? Any idea would be appreciated!


    Thursday, February 26, 2015 6:11 AM

All replies

  • Please check out my blog post on this:

    http://channel9.msdn.com/Series/Windows-Store-Developer-Solutions/Playing-Hide-and-Go-Seek-with-DataTemplated-Controls

    It's a little clunky but gets the job done.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 26, 2015 1:22 PM
    Moderator
  • Please check out my blog post on this:

    http://channel9.msdn.com/Series/Windows-Store-Developer-Solutions/Playing-Hide-and-Go-Seek-with-DataTemplated-Controls

    It's a little clunky but gets the job done.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Hi, nice video, many thanks!

    But my problem is: the DataTemplate is put in a stand-alone resource dictionay, and I don't know how to add Loaded event handler to it's root panel!(I don't want use Behaviors)

    btw, another question: I've moved the DataTemplate from a Page's resources to a stand-alone resource dictionay and add it to the MergedDictionaries in App.xaml, just based on the thought that it can have some performance gains because in doing so I can avoid recreating the DataTemplate each time the page is created. Am I right on this point? Thanks very much!


    Thursday, February 26, 2015 1:57 PM