【问题标题】:How to bind a list in store apps如何在商店应用中绑定列表
【发布时间】:2014-08-18 09:51:31
【问题描述】:

我在商店应用中的绑定列表有问题。

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;
}

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

【问题讨论】:

  • CollectionViewSource 位于何处?资源不应该在资源部分并使用 x:Key 属性作为静态资源吗?
  • CollectionViewSource 在 Page.Resources 部分。我不知道你说的 x:Key 是什么意思,CollectionViewSource 有这个属性
  • StaticResource 通常通过 x:Key 属性找到。 ResourceDictionary 使用它(x:Key) 来索引其内容。

标签: c# xaml binding microsoft-metro store


【解决方案1】:

我认为主要问题是使用 x:Name 属性而不是应该使用的 x:Key -What's the difference between x:Key and x:Name in WPF?(它适用于 wpf,但 xaml 和控件对于 win 商店应用程序几乎相同)

<CollectionViewSource x:Key="CategoriesViewSource" IsSourceGrouped="True"/>

编辑

好吧,实际上我有点困惑,因为resource by key 和其他与 WPF XAML 有关的文档说x:Key 应该用于资源,而this WinRT XAML example on MSDN 显示了x:Name 的等效用法

但是MSDN also says:

一般来说,x:Name 不应该应用在同时使用 x:键。特定现有框架的 XAML 实现具有 在 x:Key 和 x:Name 之间引入了替换概念,但那是 不推荐的做法。

所以,现在我不确定这是问题的根源。

编辑:

尝试

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>

页面是 XAML 中的主要元素。

【讨论】:

  • 仍然没有想到:(
  • 你有没有试过直接绑定集合(避开CollectionViewSource)?而且我认为如果 msbuild 无法通过键找到资源,它应该会显示错误。它是否显示任何错误或警告?
  • 是的,我试过了。依然没有。 之后是 gridViewContent.DataContext = test;我尝试手动将文本输入到 textBlock 中,但它也没有显示 - 这在 GridView 时是正常的吗?
  • 好吧,您的类别类既没有实现 INotifyPropertyChanged,也不是依赖对象或具有适当的 propertyChanged 事件
  • 那么我必须做什么?实现接口 NotifyPropertyChanged 并在我的类中实现事件 .PropertyChanged?
猜你喜欢
  • 1970-01-01
  • 2012-11-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 2013-03-07
相关资源
最近更新 更多