C# 如何绑定应用商店中的列表

C# 如何绑定应用商店中的列表,c#,xaml,binding,microsoft-metro,store,C#,Xaml,Binding,Microsoft Metro,Store,我对商店应用程序中的绑定列表有问题 public class Category { public Category(int id, string name) { this.ID = id; this.Name = name; } public int ID { get; set; } public string Name { get; set; } } 我创建了ColllecionViewSource和GridView

我对商店应用程序中的绑定列表有问题

public class Category
{
    public Category(int id, string name)
    {
        this.ID = id;
        this.Name = name;
    }

    public int ID { get; set; }
    public string Name { get; set; }
}
我创建了ColllecionViewSource和GridView

<CollectionViewSource x:Name="CategoriesViewSource" IsSourceGrouped="True"/>
<GridView ItemsSource="{Binding Source={StaticResource CategoriesViewSource}}" >
            <GridView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"></TextBlock>
                        <TextBlock Text="{Binding ID}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

在我页面的构造函数中,我将类别列表添加到CollectionViewSource

public HubPage()
{
    this.InitializeComponent();
    this.navigationHelper = new NavigationHelper(this);
    this.navigationHelper.LoadState += navigationHelper_LoadState;
List<Category> test = new List<Category>();
test.Add(new Category(1, "two"));
CategoriesViewSource.Source = test;
}
public hubbage()
{
this.InitializeComponent();
this.navigationHelper=新的navigationHelper(this);
this.navigationHelper.LoadState+=navigationHelper\u LoadState;
列表测试=新列表();
增加(新类别(1,“两”);
CategoriesViewSource.Source=测试;
}

但它不起作用。。。我做错了什么?

我认为主要问题是使用了x:Name属性,而不是应该使用的x:Key-(这是用于wpf的,但xaml和控件对于win store应用程序几乎相同)


页面是XAML中的主要元素。

CollectionViewSource位于哪里?资源不应该在resource部分中,并使用x:Key属性作为静态资源找到吗?CollectionViewSource在Page.resources部分中。我不知道你说的x:Key是什么意思,CollectionViewSource有这个atributeStaticResource通常是通过x:Key属性找到的。ResourceDictionary使用它(x:Key)索引其内容。仍然没有思考:(您是否尝试过直接绑定集合(避免使用CollectionViewSource)?我认为如果msbuild无法按键找到资源,它应该会显示一个错误。它是否显示任何错误或警告?是的,我尝试了。仍然没有。在它之后,gridViewContent.DataContext=test;我尝试手动将文本输入textBlock,但它也没有显示-这在GridView中是正常的。好吧,您的类别类会这样做既不是实现INotifyPropertyChanged,也不是依赖项对象或具有适当的propertyChanged事件我必须做什么?在我的类中实现接口NotifyPropertyChanged并实现event.propertyChanged?
<CollectionViewSource x:Key="CategoriesViewSource" IsSourceGrouped="True"/>
HubPage: Page, INotifyPropertyChanged
{
    private void OnPropertyChanged(string propName)
    { if (this.PropertyChanged != null)
          this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); }

    ...

    public HubPage()
    {
        this.InitializeComponent();
        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += navigationHelper_LoadState;
        List<Category> test = new List<Category>();
        test.Add(new Category(1, "two"));

         this.Categories = new ObservableCollection<Category>(test);
    }

    private ObservableCollection<Category> _Categories;

    ObservableCollection<Category> Categories
    {
        get {return this._Categories;}
        private set 
        {
            this._Categories = value;
            this.OnPropertyChanged("Categories");
        }
     }
}

   <Page DataContext="{Binding RelativeSource={RelativeSource Self}}" 
   ...
   <GridView ItemsSource="{Binding Categories}" >
            <GridView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"></TextBlock>
                        <TextBlock Text="{Binding ID}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </GridView.ItemTemplate>
   </GridView>