Win8 XAML如何绑定List<>到ListView

时间:2013-02-23 02:29:36

标签: xaml binding

我正在尝试在Win8应用程序中的XAML页面上填充列表视图控件。我已将以下属性添加到页面XAML:

<common:LayoutAwarePage x:Class="SecAviTools.ViewWeatherHome"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:common="using:MyNameSpace.Common"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:local="using:MyNameSpace"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                    xmlns:viewmodel="using:MyNameSpace.Win8ViewModel"
                    x:Name="pageRoot"
                    DataContext="{Binding DefaultViewModel,
                                          RelativeSource={RelativeSource Self}}"
                    mc:Ignorable="d">

<!-- ... -->

<ListView ItemsSource="{Binding Path=viewmodel:Stations}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Path=Id}"/>
                <TextBlock Text="{Binding Path=Name}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我的源类是:

namespace MyNameSpace.Win8ViewModel
{
    public class Stations : INotifyPropertyChanged, INotifyCollectionChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;
        protected void OnCollectionChanged<T>(NotifyCollectionChangedAction action, ObservableCollection<T> items)
        {
            if (CollectionChanged != null)
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, items));
        }

        public Stations()
        {
            AllStations = new ObservableCollection<Station>();
            AddStations(new List<Station>());
        }

        public ObservableCollection<Station> AllStations { get; private set; }

        public void AddStations(List<Station> stations)
        {
            AllStations.Clear();
            foreach (var station in stations)
                AllStations.Add(station);

            OnCollectionChanged(NotifyCollectionChangedAction.Reset, AllStations);
            OnPropertyChanged("AllStations");
        }
    }

    public class Station
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

页面上还有一个按钮(此处未显示)执行以下操作:

public sealed partial class MyPage : MyNameSpace.Common.LayoutAwarePage
{
    private Stations m_Stations = new Stations();

    //...

    private async void SearchButtonClick(object sender, RoutedEventArgs e)
    {
        var list = new List<Station>();
        list.Add(new Station() { Id = 0, Name = "Zero" });
        list.Add(new Station() { Id = 1, Name = "One" });

        m_Stations.AddStations(list);
    }
}

但是,当我运行代码时,列表视图中没有任何内容。我错过了什么?

TIA

1 个答案:

答案 0 :(得分:0)

您没有显示DefaultViewModel是什么,但我认为它被设置为您显示的类的实例,Stations。在这种情况下,您需要绑定到:

<ListView ItemsSource="{Binding Path=AllStations}">

绑定的路径通常是指某处的属性;没有进一步的规范,例如Source,它是对象上设置为DataContext的属性。

无论如何,您不需要命名空间限定符“viewmodel:”。

顺便说一下,如果你最终绑定到ObservableCollection,你不需要实现INotifyCollectionChanged,只需要在设置AllStations属性时实现INotifyPropertyChanged。