Reputation: 9652
Several Wpf controls have no visual representation unless they have some data (ItemsControl for example). So to view their layout in the Visual Studio designer you need to use the DesignData XAML extension to populate them with something that can be rendered. A common usage of these controls is to define several implicitly typed datatemplates so that the control can display a variety of types.
However, it seems to me that the designer has little or no support whatsoever for this most simple of layouts making it all but useless to us, but I'm hoping I'm wrong and that I've just overlooked something. Here is how it seems to me (xmlns namespaces omitted for brevity).
<UserControl
d:DataContext="{d:DesignData Source=/CustomersDesignData.xaml}">
<Grid>
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock></TextBlock>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
The above works in that the designer correctly displays the sample data enabling one to verify the layout. But there is no implicit datatyping making this approach useless for displaying multiple types.
<UserControl
d:DataContext="{d:DesignData Source=/CustomersDesignData.xaml}">
<Grid>
<ItemsControl>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:Customer}">
<ListBoxItem>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock></TextBlock>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</ListBoxItem>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
</Grid>
</UserControl>
The above works fine at runtime and additional implicitly datatyped tempates can be added enabling the control to display multiple types. The gorilla sized fly in the ointment though is that the designer dumbly displays the raw ToString() output, making this approach useless for developing the UI.
Furthermore it seems to make no difference whatsoever where you define the template, put it in UserControl or App resources and the result is the same, nada. As far as I know there is no way to support any form of conditional compilation in XAML and the type's constructor is not run by the designer so even a code behind solution seems unlikely.
We are at the start of a project porting a sizable data application that is going to require us to design about 200 UI's, and many of these UI's are buried deep in the application. The lack of any means of being able to develop these UI's with design data displayed is already hurting our productivity and is likely to get much worse.
Has anyone else encountered a similar issue and what solutions or workarounds exist?
Upvotes: 1
Views: 581
Reputation: 28728
The solution to this problem, in my experience and in my opinion, is to set the IDE to "Open XAML files in XAML view".
In other words, don't use the designer.
If you 'need' to use the designer then open Blend, draw the designs, and then integrate them into your application. You can see each View individually. You just need to view the consolidated layout within the context of a running application. The most productive WPF and Silverlight developers I've worked with use this approach.
Upvotes: 1