在XAML中为DataTemplate设计时数据

13

这可能是一个愚蠢的问题,但是是否可以定义一些示例数据作为DataContext,以便在DesignView中查看我的DataTemplate?

目前我总是必须运行我的应用程序才能看到我的更改是否有效。

例如,使用以下代码,DesignView只显示一个空列表框:

 <ListBox x:Name="standardLayoutListBox" ItemsSource="{Binding myListboxItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Grid.Column="0" Content="{Binding text1}" />
                <Label Grid.Column="1" Content="{Binding text2}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

请查看此帖子中提供的解决方案:https://dev59.com/L3I-5IYBdhLWcg3wYXL8 - Joe Mancuso
我现在已经阅读了许多示例,但我真的无法在设计时填充这个简单的Listbox。我确定我错过了什么,但我找不到原因。您能否为我的listbox提供一个可行的示例? - MTR
请参见下面我提供的示例代码。 - Joe Mancuso
我现在已经让它运行起来了,但是我不知道为什么会这样:-( 对于那些感兴趣的人,这个示例非常有帮助:http://www.st-lange.net/post/Silverlight-Tipp-der-Woche-Design-Time-Data-verwenden.aspx - MTR
2个回答

21
public class MyMockClass
{
    public MyMockClass()
    {
        MyListBoxItems.Add(new MyDataClass() { text1 = "test text 1", text2 = "test text 2" });
        MyListBoxItems.Add(new MyDataClass() { text1 = "test text 3", text2 = "test text 4" });
    }
    public ObservableCollection<MyDataClass> MyListBoxItems { get; set; }
}

public class MyDataClass
{
    public string text1 { get; set; }
    public string text2 { get; set; }
}
在你的 XAML 中,添加命名空间声明。
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
将模拟数据上下文添加到窗口/控件资源中。
<UserControl.Resources> 
    <local:MyMockClass x:Key="DesignViewModel"/> 
</UserControl.Resources>

然后修改您的ListBox以引用设计时对象

<ListBox x:Name="standardLayoutListBox" 
 d:DataContext="{Binding Source={StaticResource DesignViewModel}}"
ItemsSource="{Binding MyListBoxItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Grid.Column="0" Content="{Binding text1}" />
                <Label Grid.Column="1" Content="{Binding text2}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

4
在MyMockClass的构造函数中添加myListBoxItems = new ObservableCollection<MyDataClass>();后,没有错误提示,但是ListBox仍然为空。 - MTR
2
不要忘记在命名空间声明中设置mc:Ignorable:xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" - Mike Christian
1
这个解决方案似乎适用于数据模板直接附加到“ItemTemplate”属性的同质列表。然而,在纯MVVM解决方案中,似乎没有解决方案可以让项控件获取在资源中找到的与视图模型相关联的任何DataTemplate。 - Quark Soup
4
如果设计的DataContext引用了类型,可以排除本地资源定义,例如: d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=local:MyMockData}" - Dave Andersen
@DaveAndersen(或其他人):无论哪种方法,我都会收到错误消息:“对象引用未设置为对象的实例。” 下划线标记的错误可能是标签<local:MyMockClass ... />或参数d:DataContext ... local:MyMockClass}。 在任何一种情况下,当我键入“local:”时,Intellisense框中都会显示MyMockClass。 有什么想法吗? - Steven
显示剩余3条评论

2
当您在设计师中时,不应该去处理视图模型,因此我认为最好在XAML中包含设计时间数据而不是在C#中。请看这个简单的POCO表示:

当您在设计师中时,不应该去处理视图模型,因此我认为最好在XAML中包含设计时间数据而不是在C#中。请看这个简单的POCO表示

<ListView ItemsSource="{Binding Items}">
        <d:ListView.ItemsSource>
            <x:Array Type="{x:Type models:Monkey}">
                <models:Monkey Name="Baboon" Location="Africa and Asia"/>
                <models:Monkey Name="Capuchin Monkey" Location="Central and South America"/>
                <models:Monkey Name="Blue Monkey" Location="Central and East Africa"/>
            </x:Array>
        </d:ListView.ItemsSource>
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="models:Monkey">
                <TextCell Text="{Binding Name}"
                          Detail="{Binding Location}" />
            </DataTemplate>
        </ListView.ItemTemplate>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接