WPF中的设计时数据问题

时间:2011-02-15 10:09:01

标签: 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>

但我在设计师的空文本框中看到了。我做得不好?

2 个答案:

答案 0 :(得分:6)

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

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

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

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

答案 1 :(得分:0)

这通常用于 Windows Phone 项目,但在使用MVVM模式时从不必为设计时数据派生类。

查看:

<phone:PhoneApplicationPage     
d:DataContext="{d:DesignData ../DesignData/AvatarSampleData.xaml}" .../>

设计时间数据( AvatarSampleData.xaml ):

<local:AvatarViewModel 
    xmlns="http:schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http:schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:YourNameSpace.ViewModels"

    Name="Harald"
    Surname="Flasch">
</local:AvatarViewModel>

HTH, hfrmobile