Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF DataTemplateSelector Question

WPF DataTemplateSelector Question

Scheduled Pinned Locked Moved WPF
questioncsharpwpfdocker
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a Contacts control. On the left side is the list of contacts. I created two DataTempates for the list items in the Control's resources. Above the list are two buttons, List View and Card View. When they are selected, I want to change the data template of the list items accordingly. Here's a picture of Outlook Contacts showing the People button selected above the list. When one of the Current View buttons is selected, the list box items look different. This is what I'm looking for: [https://www.brycematheson.io/wp-content/uploads/2018/04/Image1\_Blurred-1024x713.jpg\](https://www.brycematheson.io/wp-content/uploads/2018/04/Image1\_Blurred-1024x713.jpg) I have a DataTemplateSelector, but when it' called the container property is the ListItem. How do I know in the DataTemplateSelector what view mode was selected in the ViewModel? By DataTemplateSelector

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

        ContactsView view = container as ContactsView;    //< View is Null at this point
    
        switch (view.ViewMode)
        {
            case ContactsView.ViewModes.Card:
                return element.FindResource("cardViewTemplate") as DataTemplate;
                break;
    
            case ContactsView.ViewModes.List:
                return element.FindResource("listViewTemplate") as DataTemplate;
                break;
        }
    
        return null;
    }
    

    }

    DataTemplates

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I have a Contacts control. On the left side is the list of contacts. I created two DataTempates for the list items in the Control's resources. Above the list are two buttons, List View and Card View. When they are selected, I want to change the data template of the list items accordingly. Here's a picture of Outlook Contacts showing the People button selected above the list. When one of the Current View buttons is selected, the list box items look different. This is what I'm looking for: [https://www.brycematheson.io/wp-content/uploads/2018/04/Image1\_Blurred-1024x713.jpg\](https://www.brycematheson.io/wp-content/uploads/2018/04/Image1\_Blurred-1024x713.jpg) I have a DataTemplateSelector, but when it' called the container property is the ListItem. How do I know in the DataTemplateSelector what view mode was selected in the ViewModel? By DataTemplateSelector

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

          ContactsView view = container as ContactsView;    //< View is Null at this point
      
          switch (view.ViewMode)
          {
              case ContactsView.ViewModes.Card:
                  return element.FindResource("cardViewTemplate") as DataTemplate;
                  break;
      
              case ContactsView.ViewModes.List:
                  return element.FindResource("listViewTemplate") as DataTemplate;
                  break;
          }
      
          return null;
      }
      

      }

      DataTemplates

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You've got the ListItem; you just need to use something like this function[^] to walk up the tree to find the control, and grab its DataContext property, which should be your ContactsView.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        You've got the ListItem; you just need to use something like this function[^] to walk up the tree to find the control, and grab its DataContext property, which should be your ContactsView.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        The problem is that the DataTemplateSelector gives me the ContactEntity, not the ListItem

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          You've got the ListItem; you just need to use something like this function[^] to walk up the tree to find the control, and grab its DataContext property, which should be your ContactsView.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #4

          That did it. Here's what I came up with:

          public static class VisualTreeExtensions
          {
          public static T FindParentOfType(this DependencyObject child) where T : DependencyObject
          {
          DependencyObject parentDepObj = child;
          do
          {
          parentDepObj = VisualTreeHelper.GetParent(parentDepObj);
          T parent = parentDepObj as T;
          if (parent != null) return parent;
          }
          while (parentDepObj != null);
          return null;
          }
          }

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups