【问题标题】:Error Implementing Design Time Data Template实现设计时数据模板时出错
【发布时间】:2017-08-31 21:11:21
【问题描述】:

我想在 WPF GridView 的设计窗口中显示虚拟数据,发现了一个类似的例子:Answer: Design time data for datatemplate in xaml - Stack Overflow

我创建了一个名为 WpfDataTemplate 的新 WPF 应用程序 (.NET Framework 4.6.1),并将示例中的代码 sn-ps 复制到 MainWindow.xaml.csMainWindow.xaml。所有添加都在 cmets 中注明##

意外行为:

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

  2. test text # 字符串未显示在设计器中。

  3. test text # 字符串在运行时不会显示在窗口中。

问题:

  1. 错误原因是什么?

  2. 我需要更改哪些代码才能按预期工作?


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();
        }
    }
}

【问题讨论】:

    标签: wpf xaml visual-studio-2017 datatemplate designview


    【解决方案1】:

    首先,您需要在 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; }
    }
    

    我在我的窗口中使用设计实例,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 2023-03-07
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多