嗨,我试着在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>
但我在设计师那里看到了空的文本框。我做什么不好?