views:

52

answers:

2

Hi !

Suppose you have WPF Window composed of many elements that are using DataTemplates / ControlTemplates (ItemControls ... ) But you want to see how every DataTemplate looks like in VS Designer. What more, if you define a ControlTemplate from as a Template located in another file to be able to view it with the content.

Something like MasterPage and ChildElements in ASP.NET You can always see what elements have you put together.

Is this possible in WPF too? Otherwise.. every change I make to the DataTemplate can be seen after a long-time-consuming compilation and start.

Thank you guys..

+1  A: 

Stefan,

I think that you can do this by creating Design Time class as described in the link below:

http://karlshifflett.wordpress.com/2008/10/11/viewing-design-time-data-in-visual-studio-2008-cider-designer-in-wpf-and-silverlight-projects/

Hope this helps.

CrimsonX
+1  A: 

To do this extremely simply, just set a DataContext in your constructor:

public MyUserControl()
{
#if !RELEASE
    //DataContext = new CustomerList { Customers = new [] {
    //  new Customer { Name = "Contoso", ZipCode = 12345 },
    //  new Customer { Name = "NorthWind", ZipCode = 12345 },
    //}};
#endif
  InitializeComponent();
  ...
}

Notice the fact that the code is commented out. When you want to see data, just uncomment the code. The #if !RELEASE protects you from accidentally including the sample data in your release (and from spending any CPU loading it).

If your sample data is big, just put it in XML or in a database and load it:

public MyUserControl()
{
#if !RELEASE
    //DataContext = XmlSerializerManager.Deserialize<CustomerList>(
    //  File.ReadAllBytes("CustomerSampleData.xml"));
#endif
  InitializeComponent();
  ...
}

In either case, the sample data will show up in the designer whenever you uncomment the code. When executing your application it will be replaced with the real data.

Ray Burns