Win8 XAML如何绑定列表<&燃气轮机;到列表视图

Win8 XAML如何绑定列表<&燃气轮机;到列表视图,xaml,binding,Xaml,Binding,我试图在Win8应用程序的XAML页面上填充列表视图控件。我已将以下属性添加到页面XAML中: <common:LayoutAwarePage x:Class="SecAviTools.ViewWeatherHome" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microso

我试图在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; }
    }
}
namespace MyNameSpace.Win8ViewModel
{
公共类电台:INotifyPropertyChanged、INotifyCollectionChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的无效OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共事件通知CollectionChangedEventHandler CollectionChanged;
CollectionChanged上的受保护无效(NotifyCollectionChangedAction、ObservableCollectionItems)
{
如果(CollectionChanged!=null)
CollectionChanged(此,新的NotifyCollectionChangedEventArgs(操作,项));
}
公共电台()
{
AllStations=新的ObservableCollection();
添加站点(新列表());
}
公共可观测集合所有站{get;private set;}
公共车站(列出车站)
{
所有站。清除();
foreach(变电站中的var站)
所有站点。添加(站点);
OnCollectionChanged(通知CollectionChangedAction.Reset,所有站点);
不动产变更(“所有车站”);
}
}
公营电台
{
公共int Id{get;set;}
公共字符串名称{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);
    }
}
公共密封部分类MyPage:MyNameSpace.Common.LayoutAwarePage
{
专用站m_站=新站();
//...
私有异步无效搜索按钮单击(对象发送方,路由目标)
{
var list=新列表();
Add(new Station(){Id=0,Name=“Zero”});
添加(新站点(){Id=1,Name=“One”});
m_站。添加站(列表);
}
}
但是,当我运行代码时,列表视图中没有显示任何内容。我错过了什么


TIA

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

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

绑定的路径通常指某个地方的属性;由于没有进一步的规范,例如源,它是设置为DataContext的对象上的属性

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

另外,如果最终绑定到ObservableCollection,则不需要实现INotifyCollectionChanged,仅在设置AllStations属性时为INotifyPropertyChanged