绑定不起作用
本文关键字:不起作用 绑定 | 更新日期: 2023-09-27 18:37:07
我的大多数绑定都工作正常,但只有一个只显示:Test.Models.PersonModel
我喜欢绑定到的属性("名称")在此类中。
这是我绑定的部分:
<ItemsControl ItemsSource="{Binding Persons}">
<StackPanel Margin="24, 4, 4, 4"
Orientation="Horizontal">
<TextBlock FontSize="{StaticResource PhoneFontSizeExtraLarge}"
FontFamily="{StaticResource PhoneFontFamilyLight}"
Text="{Binding Name}"
VerticalAlignment="Center"/>
</StackPanel>
</ItemsControl>
人员是 PersonModel 类型的可操作集合。这里是人物模型的代码:
public class PersonModel : INotifyPropertyChanged
{
private string _name = null;
public string Name
{
get { return _name; }
set { _name = value; NotifyPropertyChanged("Name"); }
}
private BitmapImage _profilpicture = null;
public BitmapImage ProfilPicture
{
get { return _profilpicture; }
set { _profilpicture = value; NotifyPropertyChanged("ProfilPicture"); }
}
#region PropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
您应该使用 ItemTemplate
(msdn):
<ItemsControl ItemsSource="{Binding Persons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Margin="24, 4, 4, 4"
Orientation="Horizontal">
<TextBlock FontSize="{StaticResource PhoneFontSizeExtraLarge}"
FontFamily="{StaticResource PhoneFontFamilyLight}"
Text="{Binding Name}"
VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>