【问题标题】:Design time data for datatemplate in xamlxaml 中数据模板的设计时间数据
【发布时间】:2011-12-06 22:03:27
【问题描述】:

这可能是一个愚蠢的问题,但是否可以将一些示例数据定义为 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>

【问题讨论】:

  • 请参阅这篇文章中提出的解决方案:stackoverflow.com/questions/1889966/…
  • 我已经为此阅读了很多示例,但我真的无法在设计时填充这个简单的列表框。我确定我错过了一些东西,但我不知道是什么。您可以为我的列表框提供一个工作示例吗?
  • 示例代码见我的回答。
  • 我现在正在运行它,但我不知道为什么:-( 对于那些感兴趣的人,这个示例帮助很大:st-lange.net/post/…

标签: wpf xaml datatemplate designview


【解决方案1】:
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>

【讨论】:

  • 添加后 myListBoxItems = new ObservableCollection();到 MyMockClass 的构造函数没有错误信息,但 Listbox 仍然是空的。
  • 我看不到任何其他问题。如果您真的很想获得设计时数据,我建议您研究 MEF 概念。我使用Cinch(搜索“ViewModel Design”以查看一些设计时支持)。抱歉,我无法提供更简单的解决方案。
  • 不要忘记在命名空间声明中设置 mc:Ignorable:xmlns:mc="schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
  • 此解决方案似乎适用于数据模板直接附加到“ItemTemplate”属性的同类列表。但是,对于纯 MVVM 解决方案似乎没有解决方案,您希望项目控件获取在资源中找到的可以与视图模型关联的任何 DataTemplate。
  • 如果设计DataContext引用了这样的类型,可以排除本地资源定义:d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=local:MyMockData}"
【解决方案2】:

如果你是设计师,你不应该弄乱视图模型,因此我认为最好将设计时数据包含在 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多