【问题标题】:Binding not working?绑定不起作用?
【发布时间】:2013-10-26 17:21:22
【问题描述】:

我的大多数绑定工作正常,但一个只显示: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>

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
}

【问题讨论】:

  • 你能贴出PersonModel类的相关代码吗?

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


【解决方案1】:

你应该使用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>

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 2011-10-02
    • 2018-11-07
    • 2017-11-02
    • 2011-12-25
    • 2012-02-12
    • 2012-01-17
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多