C# ItemsSource未传播设计时数据上下文
我有一个视图模型,其中包含一个项目数组:C# ItemsSource未传播设计时数据上下文,c#,wpf,datagrid,datacontext,itemssource,C#,Wpf,Datagrid,Datacontext,Itemssource,我有一个视图模型,其中包含一个项目数组: public class FooViewModel { public FooListItem[] ListItems { get; set; } ... } 我创建了一个子类作为设计时模拟,使用虚拟数据 public class FooViewModelMock : FooViewModel { public FooViewModelMock() { ListItems = <test data p
public class FooViewModel
{
public FooListItem[] ListItems { get; set; }
...
}
我创建了一个子类作为设计时模拟,使用虚拟数据
public class FooViewModelMock : FooViewModel
{
public FooViewModelMock()
{
ListItems = <test data population...>
...
}
}
这让我恢复了智能感知,但我丢失了模拟数据
有没有办法让设计时数据上下文通过ItemsSource绑定向下传播,并保留intellisense和design view数据
据我所知,这是Visual Studio中的一个缺陷。下面是我如何解决这个问题的 首先,我不使用DesignInstance,因为我无法让它在Visual Studio 2013中工作。我用的是:
d:DataContext="{x:Static userControls:[ insert class name here ]DesignerData.Example}"
示例是创建…DesignerData类实例的静态属性。我不知道为什么会这样,但是DesignInstance应该做完全相同的事情,却没有。我试图指定IsDesignTimeCreatable,但没有帮助
此类必须具有必需的collection属性,在我的示例中,该属性仅返回匿名类型对象形式的数据:
public IEnumerable<object> Elements
{
get
{
return new object[]
{
new { ... },
new { ... },
new { ... }
};
}
}
但是请注意,这只是为了避免警告和自动完成。这些将永远不会被真正阅读
这种方法的一大潜在缺点是,如果一个集合中的元素与另一个集合中的元素具有相同名称但类型不同的属性,则不能使用这种方法
这也是笨拙和不方便的,因为变通办法是习惯的
d:DataContext="{d:DesignInstance models:FooListItem}"
d:DataContext="{x:Static userControls:[ insert class name here ]DesignerData.Example}"
public IEnumerable<object> Elements
{
get
{
return new object[]
{
new { ... },
new { ... },
new { ... }
};
}
}
public string Key { get; private set; }
public Element Value { get; private set; }