Ok,
I am currently tidying up all my old stuff. I ran into the issue of "code only DataTemplates" - which are really a pain in the ass. You can't see anything, they are really hard to design, and I want to improve my project.
So I had the idea to use the following solution. The main benefits are:
You have designer support for your data template
You can easily include example sample data
The file naming is consistent and easy to remember
The preview does not require an additional XAML wrapper (even with code only controls)
I will try to explain and illustrate my solution using a few pictures. I am interested in feedback, especially if you can imagine a better way to do it. And, of course, if you see any maintenance or performance issues.
Ok, lets start with a simple PreviewObject. I want to have some data in it, so I create a subclass which will automatically fill in some dummy data. Then I add a list to the control, and name this list. Afterwards I add a DataTemplate, this is the sole reason for the whole control (to be able to see and edit the DataTemplate in place):
Now I use this control to get my DataTemplate, to use it in other places. To make this easier, I added some code in the code behind, see here:
Now I want a control to show me a list of PreviewItems, so I created a "code-only" control which creates an instance of my service (or gets one using DI in real world) and fills its list box with it:
To view the result of this work, I added this control inside the same named XAML, this is basically only to be able to see the final result:
What I do not like in this solution: The need to create the last control in "code only". So I tried something different while writing this post. The following two screenshots illustrate the approach. I am creating an instance of the service inside the DataContext, and I am using bindings to supply the Itemssourc and the ItemTemplate.
The reason for the strange "static property" is refactoring support. If I hardcode the path in the designer (e.g. using "Path = PreviewHistory") and I refactor the names (which happens quite often, early design phase) - I screw up my controls without realizing it.
Does anyone has a better idea for this? I am using Resharper, btw.
Thanks for any input, and sorry for the image overkill. Just easier to explain that way..
Chris