实施设计时数据模板时出错 [英] Error Implementing Design Time Data Template

查看:58
本文介绍了实施设计时数据模板时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF GridView 的设计窗口中显示伪数据,并找到一个类似的示例:答案:xaml中数据模板的设计时间数据-堆栈溢出

I want to display dummy data in the design window of a WPF GridView and found a similar example: Answer: Design time data for datatemplate in xaml - Stack Overflow

我创建了一个新的WPF应用程序(.NET Framework 4.6.1)名为 WpfDataTemplate ,并在示例中的代码段中将其复制到 MainWindow.xaml.cs MainWindow.xaml 。所有添加的内容都在带有 ## 的注释中注明。

I created a new WPF App (.NET Framework 4.6.1) named WpfDataTemplate and copied in the code snippets from the example into MainWindow.xaml.cs and MainWindow.xaml. All additions are noted in comments with ##.

意外行为:


  1. < local:MyMockClass x:Key = DesignViewModel /> 产生错误对象引用未设置为对象的实例。

  1. The line <local:MyMockClass x:Key="DesignViewModel" /> produces the error "Object reference not set to an instance of an object."

测试文本#字符串未显示在设计器中。

The test text # strings are not displayed in the Designer.

测试文本#字符串未显示在运行时显示在窗口中。

The test text # strings are not displayed in the Window at runtime.

问题:


  1. 错误原因是什么?

  1. What is the cause of the error?

我需要更改什么?使代码按预期工作?

What do I need to change for the code to work as expected?






MainWindow .xaml

<Window x:Class="WpfDataTemplate.MainWindow"
    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"
    xmlns:local="clr-namespace:WpfDataTemplate"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<!-- ## BEGIN ADD -->
<Window.Resources>
    <local:MyMockClass x:Key="DesignViewModel" />
</Window.Resources>
<!-- ## END ADD -->
<Grid>
    <!-- ## BEGIN ADD -->
    <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>
    <!-- ## END ADD -->
</Grid>

MainWindow.xaml .cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
// ## BEGIN ADD
using System.Collections.ObjectModel; 
// ## END ADD

namespace WpfDataTemplate
{
    // ## BEGIN ADD
    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; }
    }
    // ## END ADD

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}


推荐答案

首先,您需要在 MyMockClass 中初始化集合。

Firstly you need to initialise your collection in your MyMockClass.

public class MyMockClass
{
    public MyMockClass()
    {
        MyListBoxItems = new ObservableCollection<MyDataClass>()
        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; }
}

我在Window中使用设计实例,如下所示:

I use the design instance in my Window like so:

d:DataContext="{d:DesignInstance Type=local:MyMockClass, IsDesignTimeCreatable=True}"

这篇关于实施设计时数据模板时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆