wpf - WPF 中设计时数据的问题

标签 wpf visual-studio-2010 design-time-data

嗨,我尝试在 wpf 中使用我的第一个设计时数据。我使用以下教程:

http://karlshifflett.wordpress.com/2009/10/21/visual-studio-2010-beta2-sample-data-project-templates/

http://karlshifflett.wordpress.com/2009/10/28/ddesigninstance-ddesigndata-in-visual-studio-2010-beta2/

我创建了简单的数据类,如下:

public class Avatar:INotifyPropertyChanged
    {
        private string _name;
        private string _surname;

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        public string Surname
        {
            get { return _surname; }
            set
            {
                _surname = value;
                NotifyPropertyChanged("Surname");
            }
        }


        public new event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

    }

然后我创建了示例数据:

<TestForDesignTimeData:Avatar xmlns:TestForDesignTimeData="clr-namespace:TestForDesignTimeData" Name="John" Surname="Smith"/>

并尝试在 wpf 窗口中使用设计时数据:

<Window x:Class="TestForDesignTimeData.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:TestForDesignTimeData="clr-namespace:TestForDesignTimeData"
        mc:Ignorable="d" 
        Title="MainWindow" Height="350" Width="525">

        <StackPanel d:DataContext="{d:DesignInstance TestForDesignTimeData:Avatar}">
            <TextBlock Background="Yellow" Height="40" Width="250"   Text="{Binding Path=Name}"/>
            <TextBlock Background="Yellow" Height="40" Width="250"   Text="{Binding Path=Surname}"/>
    </StackPanel>
</Window>

但我在设计器中看到空文本框。我做错了什么?

最佳答案

您需要从 Avatar 创建一个将在设计时使用的派生类,并在此类的构造函数中定义示例数据:

public class AvatarDesignTime : Avatar
{
   public AvatarDesignTime()
   {
      Name = "John";
      Surname = "Smith";
   }
}

然后,您需要为 DesignInstance 指定 IsDesignTimeCreatable=True 以便在设计时启用实例创建(否则您指定的类型仅用于有关类型成员的信息,以便在设计时间):

<StackPanel d:DataContext="{d:DesignInstance TestForDesignTimeData:AvatarDesignTime, IsDesignTimeCreatable=True}">

关于wpf - WPF 中设计时数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5002247/

相关文章:

wpf - 如何知道窗口是否被 "x"按钮关闭?

c# - 如何在关闭显示器时调用方法以及通过按下显示器上的按钮打开显示器

wpf - 我只需要一个显示多行的简单 WPF ToolTip 样式

xaml - 在 MAUI 中进行设计 - 设计时 BindingContext

c# - 在 WPF 中,您可以在没有代码隐藏的情况下过滤 CollectionViewSource 吗?

asp.net - 为什么对 aspnet_compiler 的调用不会生成新的 .NET 程序集?

c - 指针和取消引用

c# - 如何通过按钮控制将 Fez Panda II 设置为 'Sleep' 模式?

xaml - UWP X :Bind and Design Time Data

wpf - 来自外部 XML 文件的设计时数据