WPF 中设计时数据的问题
您好,我尝试在 wpf 中使用我的第一个设计时数据。我使用以下教程:
我创建了简单的数据类,如下:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要从
Avatar
创建一个将在设计时使用的派生类,并在此类的构造函数中定义示例数据:然后您需要指定
IsDesignTimeCreatable=True
让 DesignInstance 在设计时启用实例创建(否则您指定的类型仅用于有关类型成员的信息,以便在设计时设置绑定):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: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):Windows Phone 项目经常这样做,但在使用 MVVM 模式时从未需要为设计时数据派生类。
查看:
设计时数据 (AvatarSampleData.xaml):
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:
Design Time Data (AvatarSampleData.xaml):
hth,
hfrmobile