Silverlight: Code Pattern for sample data in designer?

Status
Not open for further replies.

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
In WPF, I've built a habit when I'm building a UI to have some sample data that I bind in the XAML to a ListBox.ItemsSource so that I can see what the list will look like in the designer. In WPF, I would do it by making a static property that returns the fake data and then use it like this:
Code:
<ListBox Name="lbCustomers" ItemsSource="{x:Static MyClass.FakeCustomerForDesignView}" />
and it would work fine for displaying sample data in the designer.

I tried to do the same thing in Silverlight, and quickly found that x:Static isn't supported in Silverlight. (I'm just transistioning from WPF for some Silverlight work so I might be making newb SL mistakes.) I modified it to the following: (customerPage is the name of the "MyClass" class element in the XAML)
Code:
<ListBox Name="lbCustomers" ItemsSource="{Binding ElementName=customerPage, Path=FakeCustomerForDesignView}" />

This binding works if I run the app, but doesn't work in the designer. (Which defeats much of the purpose of doing it to make designing easier/faster.)

Is there a Silverlight friendly code pattern to get sample data in the designer? (NOTE: Sample data is datatypes that cannot be represented in XAML, so I can't just make sample data in XAML.)
 

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
allotrope":2e345ywf said:
What about your sample data is inexpressible in XAML?
It isn't "inexpressible," I'm just lazy.

Typically I've got raw data somewhere in a non-XAML form already created somewhere, and I want a least-cost method of making a sample show up in the designer. If I've got to go to the work of converting the sample data to XAML, it becomes more work than the pay-off of having it show up in the designer for a few hours while I'm designing. IME, sample data in the designer is "worth" about 10-15 minutes max effort, anything more is just too much effort for the payout. In WPF, I could get sample data in the designer in about 5 minutes with my old pattern.

I'd hope there was someway to get data into Silverlight with just as little effort.
 

allotrope

Smack-Fu Master, in training
66
Silverlight doesn't have x:Static so you just need to declare an instance of the sample data class in your resources.

Code:
<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:SlSampleData="clr-namespace:SlSampleData"
	x:Class="SlSampleData.MainPage"
	Width="640" Height="480">

	<UserControl.Resources>
		<SlSampleData:MyClass x:Key="myClass"/>
	</UserControl.Resources>

	<Grid x:Name="LayoutRoot" Background="White">
		<ListBox Name="lbCustomers" DataContext="{StaticResource myClass}"  ItemsSource="{Binding FakeCustomerForDesignView}" />
	</Grid>

</UserControl>
Code:
	using System.Collections.Generic;

	public class MyClass
	{
		public IEnumerable<string> FakeCustomerForDesignView
		{
			get
			{
				yield return "foo";
				yield return "bar";
				yield return "qux";
			}
		}
	}
 

allotrope

Smack-Fu Master, in training
66
Doesn't work in that you can't see the items in the ComboBox?

If that's it, you'll have to type the IsDropDownOpen="True" bit into the xaml code by hand (since the Properties panel doesn't expose that one):
Code:
		<ComboBox x:Name="lbCustomers"
			IsDropDownOpen="True" 
			DataContext="{Binding Source={StaticResource sampleData}}" 
			ItemsSource="{Binding ComboBoxSampleData}" 
			/>
And you might have to re-type that (ie, cut it and paste it back) once in awhile. 2010's designer doesn't seem to enforce it too well.

edit to add:
Blend is a significantly better designer for Silverlight, imo.
 

svdsinner

Ars Legatus Legionis
15,152
Subscriptor
The other really annoying issue is that Visual Studio pops up a null reference error on start up when you do this, and the designer won't load until you build the solution for the first time after opening. Not sure why Visual Studio has issues with referencing a class before the solution is built the first time VS is open. (IMHO, there are far, far too many ways to crash VS or cause errors to pop up in VS with fairly normal coding patterns in WPF/Silverlight)
 

QuadDamaged

Ars Praefectus
3,983
Subscriptor++
svdsinner":2tyac04j said:
The other really annoying issue is that Visual Studio pops up a null reference error on start up when you do this, and the designer won't load until you build the solution for the first time after opening. Not sure why Visual Studio has issues with referencing a class before the solution is built the first time VS is open. (IMHO, there are far, far too many ways to crash VS or cause errors to pop up in VS with fairly normal coding patterns in WPF/Silverlight)

I would recommend not using VS.Net's designer at all and switching to Blend for cases where design-time is important.

To get rid of the designer: right click on a xaml file in the solution explorer, select 'Open with...', the 'Source Code (Text)' and click 'Default'
 
Status
Not open for further replies.