2010-11-18 78 views
1

在混合4中,我试图从我的虚拟机类生成示例数据源。该类有一个属性,该属性返回一个接口的observablecollection,另一个属性具有observable集合的一个类。生成样本数据源时,Blend会为类属性生成数据,但不生成接口。有没有解决的办法?我的代码绝对需要有接口,但同样我想能够看到为设计时间生成的示例数据。混合不生成虚拟机的示例数据

+0

我有同样的问题,我VM有一个接口属性,Blend也不在绑定窗口中显示属性。 – TDaver 2011-04-04 16:35:18

+0

你能举个例子吗? – 2011-04-04 22:48:25

+0

@Michael S. Scherotter:'public class PartialViewModel {public M Model {get; private set;}}'然后我的DataContext是'public class MyVM {public PartialViewModel Partial {get; private set;}}'当然''public interface IDataInterface {string Stuff {get; set;}}'我的目标是在混合中看到Partial.Model.Stuff属性。但东西没有出现,并且提到的原始问题也没有得到样本数据! – TDaver 2011-04-05 06:09:21

回答

2

这里的问题是Blend不知道要创建什么样的对象作为IDataInterface的具体实现。我会建议建立两个设计时的数据源,一个用于MyVM,一个用于混凝土IDataInterface实现:

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; 
     } 
    } 
} 

,然后将XAML是:

<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> 
+0

我会试试这个!谢谢! – TDaver 2011-04-06 07:47:21

+0

我试过了,问题是我不想将Partial.Model分配给内部网格的DataContext,因为我需要Model和MyVM的属性,但是暂时用MyVM中的具体类替换接口工作(如约翰内斯建议的) – TDaver 2011-04-10 06:32:24

+0

好的,你的解决方案并不完美,但我没有真正指定小的细节,所以我会奖励赏金。再次感谢。 – TDaver 2011-04-11 09:03:32