I have an area on the left with selectable items, and depending on what type of item is selected, I want to show one of three or four different forms on the right.
It would be nice to make some type of control so the main form can be less of a mess.
What’s the best pattern for this in WPF?
I’m a WPF newbie, and I’ve spent some time going in some obviously incorrect directions. Thanks!
I would say, whenever possible Templates are the way to go in WPF. They define how controls or data is displayed in the UI. Using templates, it is possible to use any object (not only strings) as the content of a Button, for example. If you set the content of a Button to be an object of type
MyType, WPF will look for a DataTemplate forMyTypein the resources and use that one if it is found. If no DataTemplate is found, it will use theToString()method of that object and display the result.In your scenario, you could use a simple
ContentControlfor your details view on the right and define differentDataTemplates for each item type. If not every item needs a different template (i.e. some types share the same template), you could implement aContentTemplateSelectorto determine the correctDataTemplateprogrammatically.The Data Templating Overview gives a good introduction into that topic.
HTH, good luck!