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. Switch WPF UI at runtime

Switch WPF UI at runtime

Scheduled Pinned Locked Moved WPF
csharpwpfdesigndata-structuresregex
6 Posts 4 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.
  • E Offline
    E Offline
    Ed Hill _5_
    wrote on last edited by
    #1

    I have a set of data that can be defined by the user, this will consist of item types i provide to them, for example (Text/Date/Decimal/Address/Lookup/Group). A group allows them to put related data together, groups can be nested as deep as the user requires. Think Composite pattern. As every user is able to create their own structure how it is best displayed will also vary from user to user. I would like to be able to provide a mechanism that allows the user to select how they want their data displayed. To do this i have created a ResourceDictionary for each way of displaying the data but i've struggled to find a good way to implement switching between display methods. I have looked into switching the ResourceDictionary in code but could not find a good way to implement this. I've also thought about implementing a DataTemplateSelector and again struggled to get this working. I tried creating empty interfaces (IShowAsTree, IShowAsTabs, IShowAsGroupBoxes), implementing them all in my view that holds the data, and casting when i return the property they are bound to.

    public interface IBaseView{}
    public interface IShowAsTree:IBaseView{}
    public interface IShowAsTabs:IBaseView{}
    public interface IShowAsGroupBoxes:IBaseView{}

    public class DataCollection:BaseVM, IShowAsTree, IShowAsTabs, IShowAsGroupBoxes
    {
    //extended data class
    }

    public class ExtenededDataView:BaseVM
    {
    public String[] ViewAsOptions{get{return new[] {"Tree","Tabs","GroupBoxes"}}

    private String \_viewAs;
    public String ViewAs
    {
        get {return \_viewAs;}
        set
        {
            \_viewAs = value;
            OnPropertyChanged(()=>View);
        }
    }
    private DataCollection \_view;
    public IBaseView View
    {
        get
        {
            switch(ViewAs)
            {
                case "Tree":
                return \_view as IShowTabs;
                break;
                //...
            }
        }
    }
    

    }

    This failed, should have known but DataTemplating appears to only work on classes not interfaces. This is when i decided on quite a hack for a solution, replace the interfaces with classes, and put a property in the parent class that holds the DataCollection. This works but feels wrong, so if any one can advice me of a better solution, or an area to research more it would be appreciated. Thanks to anyone who read this far, code was typed in so may well contain the odd error, but hop

    K A P 3 Replies Last reply
    0
    • E Ed Hill _5_

      I have a set of data that can be defined by the user, this will consist of item types i provide to them, for example (Text/Date/Decimal/Address/Lookup/Group). A group allows them to put related data together, groups can be nested as deep as the user requires. Think Composite pattern. As every user is able to create their own structure how it is best displayed will also vary from user to user. I would like to be able to provide a mechanism that allows the user to select how they want their data displayed. To do this i have created a ResourceDictionary for each way of displaying the data but i've struggled to find a good way to implement switching between display methods. I have looked into switching the ResourceDictionary in code but could not find a good way to implement this. I've also thought about implementing a DataTemplateSelector and again struggled to get this working. I tried creating empty interfaces (IShowAsTree, IShowAsTabs, IShowAsGroupBoxes), implementing them all in my view that holds the data, and casting when i return the property they are bound to.

      public interface IBaseView{}
      public interface IShowAsTree:IBaseView{}
      public interface IShowAsTabs:IBaseView{}
      public interface IShowAsGroupBoxes:IBaseView{}

      public class DataCollection:BaseVM, IShowAsTree, IShowAsTabs, IShowAsGroupBoxes
      {
      //extended data class
      }

      public class ExtenededDataView:BaseVM
      {
      public String[] ViewAsOptions{get{return new[] {"Tree","Tabs","GroupBoxes"}}

      private String \_viewAs;
      public String ViewAs
      {
          get {return \_viewAs;}
          set
          {
              \_viewAs = value;
              OnPropertyChanged(()=>View);
          }
      }
      private DataCollection \_view;
      public IBaseView View
      {
          get
          {
              switch(ViewAs)
              {
                  case "Tree":
                  return \_view as IShowTabs;
                  break;
                  //...
              }
          }
      }
      

      }

      This failed, should have known but DataTemplating appears to only work on classes not interfaces. This is when i decided on quite a hack for a solution, replace the interfaces with classes, and put a property in the parent class that holds the DataCollection. This works but feels wrong, so if any one can advice me of a better solution, or an area to research more it would be appreciated. Thanks to anyone who read this far, code was typed in so may well contain the odd error, but hop

      K Offline
      K Offline
      Kenneth Haugland
      wrote on last edited by
      #2

      Im a little confused as to what exactly you are trying to do. Do you want to creat a custom "user control" of sorts?

      E 1 Reply Last reply
      0
      • E Ed Hill _5_

        I have a set of data that can be defined by the user, this will consist of item types i provide to them, for example (Text/Date/Decimal/Address/Lookup/Group). A group allows them to put related data together, groups can be nested as deep as the user requires. Think Composite pattern. As every user is able to create their own structure how it is best displayed will also vary from user to user. I would like to be able to provide a mechanism that allows the user to select how they want their data displayed. To do this i have created a ResourceDictionary for each way of displaying the data but i've struggled to find a good way to implement switching between display methods. I have looked into switching the ResourceDictionary in code but could not find a good way to implement this. I've also thought about implementing a DataTemplateSelector and again struggled to get this working. I tried creating empty interfaces (IShowAsTree, IShowAsTabs, IShowAsGroupBoxes), implementing them all in my view that holds the data, and casting when i return the property they are bound to.

        public interface IBaseView{}
        public interface IShowAsTree:IBaseView{}
        public interface IShowAsTabs:IBaseView{}
        public interface IShowAsGroupBoxes:IBaseView{}

        public class DataCollection:BaseVM, IShowAsTree, IShowAsTabs, IShowAsGroupBoxes
        {
        //extended data class
        }

        public class ExtenededDataView:BaseVM
        {
        public String[] ViewAsOptions{get{return new[] {"Tree","Tabs","GroupBoxes"}}

        private String \_viewAs;
        public String ViewAs
        {
            get {return \_viewAs;}
            set
            {
                \_viewAs = value;
                OnPropertyChanged(()=>View);
            }
        }
        private DataCollection \_view;
        public IBaseView View
        {
            get
            {
                switch(ViewAs)
                {
                    case "Tree":
                    return \_view as IShowTabs;
                    break;
                    //...
                }
            }
        }
        

        }

        This failed, should have known but DataTemplating appears to only work on classes not interfaces. This is when i decided on quite a hack for a solution, replace the interfaces with classes, and put a property in the parent class that holds the DataCollection. This works but feels wrong, so if any one can advice me of a better solution, or an area to research more it would be appreciated. Thanks to anyone who read this far, code was typed in so may well contain the odd error, but hop

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        I don't find the interface approach incorrect. Using interfaces to implement more complex templating sounds like a valid pattern to me.

        A new Windows Phone App - Speed Dial

        E 1 Reply Last reply
        0
        • K Kenneth Haugland

          Im a little confused as to what exactly you are trying to do. Do you want to creat a custom "user control" of sorts?

          E Offline
          E Offline
          Ed Hill _5_
          wrote on last edited by
          #4

          I'm trying to allow the user to switch which DataTemplate is used for an object based on a choice by the user. At a very basic level consider an oject you have written called MyObject:

          That resource Dictionary provides 3 different DataTemplates for the same object, what i want is a mechanism to let the user pick which one gets used, but in the actual situation things are a little more complex, but once i sort the right way to do the switching then i'll be able to implement that for the more complex situation.

          1 Reply Last reply
          0
          • A Abhinav S

            I don't find the interface approach incorrect. Using interfaces to implement more complex templating sounds like a valid pattern to me.

            A new Windows Phone App - Speed Dial

            E Offline
            E Offline
            Ed Hill _5_
            wrote on last edited by
            #5

            It did also appear valid to me, but when the data templates were setup with a DataType={x:Type Interfaces:IShowTree}, the template was not applied and the name of the view model was shown. A little google for "DataTemplate Interface" returns lots of results all saying that DataTemplates don't work with interfaces. The main reason seems to be classes that imlement multiple interfaces would be difficult for WPF to pick the right DataTemplate to use.

            1 Reply Last reply
            0
            • E Ed Hill _5_

              I have a set of data that can be defined by the user, this will consist of item types i provide to them, for example (Text/Date/Decimal/Address/Lookup/Group). A group allows them to put related data together, groups can be nested as deep as the user requires. Think Composite pattern. As every user is able to create their own structure how it is best displayed will also vary from user to user. I would like to be able to provide a mechanism that allows the user to select how they want their data displayed. To do this i have created a ResourceDictionary for each way of displaying the data but i've struggled to find a good way to implement switching between display methods. I have looked into switching the ResourceDictionary in code but could not find a good way to implement this. I've also thought about implementing a DataTemplateSelector and again struggled to get this working. I tried creating empty interfaces (IShowAsTree, IShowAsTabs, IShowAsGroupBoxes), implementing them all in my view that holds the data, and casting when i return the property they are bound to.

              public interface IBaseView{}
              public interface IShowAsTree:IBaseView{}
              public interface IShowAsTabs:IBaseView{}
              public interface IShowAsGroupBoxes:IBaseView{}

              public class DataCollection:BaseVM, IShowAsTree, IShowAsTabs, IShowAsGroupBoxes
              {
              //extended data class
              }

              public class ExtenededDataView:BaseVM
              {
              public String[] ViewAsOptions{get{return new[] {"Tree","Tabs","GroupBoxes"}}

              private String \_viewAs;
              public String ViewAs
              {
                  get {return \_viewAs;}
                  set
                  {
                      \_viewAs = value;
                      OnPropertyChanged(()=>View);
                  }
              }
              private DataCollection \_view;
              public IBaseView View
              {
                  get
                  {
                      switch(ViewAs)
                      {
                          case "Tree":
                          return \_view as IShowTabs;
                          break;
                          //...
                      }
                  }
              }
              

              }

              This failed, should have known but DataTemplating appears to only work on classes not interfaces. This is when i decided on quite a hack for a solution, replace the interfaces with classes, and put a property in the parent class that holds the DataCollection. This works but feels wrong, so if any one can advice me of a better solution, or an area to research more it would be appreciated. Thanks to anyone who read this far, code was typed in so may well contain the odd error, but hop

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              The easy way to do this is to use a trick I describe here[^]. Basically, you don't show them as DataTemplates. What you do is to convert your DataTemplates into ControlTemplates, and then have a single DataTemplate that uses DataTriggers to swap templates depending on the underlying data.

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              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