WPF 中设计时数据的问题

发布于 2024-10-17 05:35:10 字数 2514 浏览 0 评论 0原文

您好,我尝试在 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>

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

Hi I try use my first design time data in wpf. I use tutorial from:

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/

I create simple data class, here is it:

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

    }

Then I created sample data:

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

And try use design time data in wpf window:

<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>

But I see in designer empty textboxes. What I do bad?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深者入戏 2024-10-24 05:35:10

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

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

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

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

You need to create a derived class from Avatar that will be used in the design time and define the sample data in the constructor of this class:

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

Then you need to specify IsDesignTimeCreatable=True for the DesignInstance to enable instance creation in the design time (otherwise the type that you specify is used just for information about type members in order to setup bindings in the design time):

<StackPanel d:DataContext="{d:DesignInstance TestForDesignTimeData:AvatarDesignTime, IsDesignTimeCreatable=True}">
2024-10-24 05:35:10

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,
高频移动

Did this often for Windows Phone projects but never had to derive a class for design time data when using the MVVM pattern.

View:

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

Design Time Data (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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文