Blend doesn't generate sample data for VM
In Blend 4, i am t开发者_如何学运维rying to generate sample data source from my VM class. The class has a property that returns observablecollection of an interface and another property with observablecollection of a class. When generating sample data source, Blend generates data for the class property but not the interface. Is there a way around this? My code absolutely requires to have the interface, but at the same i want to able to see the sample data generated for design time.
The problem here is that Blend doesn't know what kind of object to create as the concrete implementation of IDataInterface. I would suggest creating two design-time data sources, one for the MyVM and one for the concrete IDataInterface implementation:
namespace SilverlightApplication1
{
public interface IDataInterface
{
string Stuff { get; set; }
}
public class PartialViewModel<M>
{
public M Model { get; private set; }
}
public class ConcreteDataInterface : IDataInterface
{
public ConcreteDataInterface()
{
this.Stuff = "Concrete Stuff!";
}
public string Stuff {get;set;}
}
public class MyVM
{
public PartialViewModel<IDataInterface> Partial
{
get;
private set;
}
}
}
and then the XAML would be:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot"
d:DataContext="{d:DesignData /SampleData/MyVMSampleData.xaml}">
<Grid DataContext="{Binding Partial.Model}"
d:DataContext="{d:DesignData /SampleData/ConcreteDataInterfaceSampleData.xaml}">
<TextBlock Text="{Binding Stuff}"/>
</Grid>
</Grid>
</UserControl>
精彩评论