[英]How to bind a list in store apps
I have problem with binding list in store apps. 我在商店应用程序中的绑定列表有问题。
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; }
}
I created ColllecionViewSource and GridView 我创建了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>
In constructor of my page i add list of Category to CollectionViewSource 在页面的构造函数中,我将类别列表添加到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;
}
But it doesn't work... What i do wrong? 但这不起作用...我做错了什么?
I think that the main problem is the use of x:Name attribute instead of x:Key that is supposed to be used - What's the difference between x:Key and x:Name in WPF? 我认为主要问题是使用x:Name属性而不是应该使用的x:Key-WPF中x:Key和x:Name有什么区别? (it is for wpf but xaml and controls are nearly the same for win store apps)
(适用于wpf,但xaml和控件与win store应用程序几乎相同)
<CollectionViewSource x:Key="CategoriesViewSource" IsSourceGrouped="True"/>
EDIT 编辑
Well, actually I am bit confused, because resource by key and other docs pertained for WPF XAML say that x:Key
should be used for resources, while this WinRT XAML example on MSDN shows the equivalent use of x:Name
好吧,实际上我有点困惑,因为按键分配资源以及与WPF XAML相关的其他文档都说
x:Key
应该用于资源,而MSDN上的WinRT XAML示例显示了x:Name
的等效用法
But MSDN also says : 但是MSDN也说 :
In general, x:Name should not be applied in situations that also use x:Key.
通常,在也使用x:Key的情况下不应应用x:Name。 XAML implementations by specific existing frameworks have introduced substitution concepts between x:Key and x:Name, but that is not a recommended practice.
通过特定的现有框架实现的XAML实现在x:Key和x:Name之间引入了替换概念,但是不建议这样做。
So, now I am not sure that it is source of problems. 因此,现在我不确定这是问题的根源。
EDIT: 编辑:
Try to 尝试
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>
Page is your main element in XAML. 页面是XAML中的主要元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.