C# wpf中的设计数据

C# wpf中的设计数据,c#,wpf,xaml,mocking,C#,Wpf,Xaml,Mocking,我有一个usercontrol和viewmodel,其中包含一个属性报告。usercontrol的datacontext绑定到viewmodel 在控件中,我有一个列表框,列表框绑定到属性报告 <ListBox x:Name="ReportListBox" ItemsSource="{Binding Reports}" ItemTemplate="{StaticResource reportItemTemplate}"

我有一个usercontrol和viewmodel,其中包含一个属性报告。usercontrol的datacontext绑定到viewmodel

在控件中,我有一个列表框,列表框绑定到属性报告

<ListBox x:Name="ReportListBox" ItemsSource="{Binding Reports}"

                 ItemTemplate="{StaticResource reportItemTemplate}"
                 IsSynchronizedWithCurrentItem="True"
                 Visibility="Visible" SelectionMode="Single">
</ListBox>

我想要的是一些设计数据,所以我创建了一个像这样的xaml文件

<m:Reports xmlns:m="clr-namespace:MYAPP.Modules.ReportList.Models">
    <m:Report ReportName="Reportname 1" Id="AAAA-BBB-CCC" ></m:Report>
    <m:Report ReportName="Reportname 2" Id="AAAA-BBB-CCC" ></m:Report>
</m:Reports>

如果我这样做,VS设计中不会显示任何内容。如果我将列表框的绑定更改为

 <ListBox x:Name="ReportListBox" ItemsSource="{Binding}"

获取设计时数据的一种方法是在代码中创建两个视图模型。一个是您的正常运行时视图模型。第二种方法是使用虚拟数据进行存根。然后,在xaml中,您可以在设计器中通过设置带有混合名称空间前缀的Datacontext来选择性地设置数据上下文

<Page
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    d:DataContext="{StaticResource DummyViewModel}">

然后在资源中创建对象的实例

<Page.Resources>
    <local:DummyViewModelClass x:Key="DummyViewModel">
</Page.Resources>

这是一个快速原型化的简单解决方案,但不一定能很好地扩展

另一种方法是在代码中识别您处于设计模式,并通过使用添加到类中的某些帮助器方法更改视图模型的填充方式。下面是该方法的一个示例


谢谢您的回答,我尝试了您的第一个解决方案,但我能让它工作的唯一方法是将d:DataContext放入列表框中。如果我把它放在页面上,或者在我的例子中是UserContrl,那么它会说“资源无法解析”
<Page.Resources>
    <local:DummyViewModelClass x:Key="DummyViewModel">
</Page.Resources>