绑定不起作用?

时间:2013-10-26 17:21:22

标签: c# xaml data-binding windows-phone-8 bind

我的大多数绑定工作正常,但只显示一个:Test.Models.PersonModel

我想要绑定的属性(“Name”)在这个类中。

这里是我绑定的部分:

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

Persons是PersonModel类型的OberservableCollection。这里是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
}

1 个答案:

答案 0 :(得分:0)

您应该使用ItemTemplatemsdn):

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