0

我正在尝试在 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

4

1 回答 1

0

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

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

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

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

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

于 2013-02-25T15:55:37.050 回答