简体   繁体   English

Silverlight Windows Phone数据绑定-noob问题

[英]Silverlight Windows Phone Databinding — noob question

I have a basic Windows Phone List application, with code like this in the MainViewModel class 我有一个基本的Windows Phone List应用程序,在MainViewModel类中具有类似的代码

// CODE THAT WORKS --

Items.Clear();

foreach (var itm in e.Result)
    Items.Add(itm);

Count = Items.Count;

// CODE THAT DOES NOT WORK -- I'm trying to understand WHY

Items = e.Result;

The databinding Xaml looks like this: 数据绑定Xaml如下所示:

<DataTemplate>
    <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
        <Image x:Name="ItemImage" Source="/AppName;component/Images/ArrowImg.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
        <StackPanel>
            <TextBlock x:Name="ItemText" Text="Event Name" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
            <TextBlock x:Name="DetailsText" Text="{Binding Path=Description}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
        </StackPanel>
    </StackPanel>
</DataTemplate>

I think I have a misunderstanding of how ObservableCollection and INotifyPropertyChanged work, because I'm thinking that this code should work. 我认为我对ObservableCollection和INotifyPropertyChanged的工作方式有误解,因为我认为这段代码应该工作。 Databinding to NonCollection items is working as I'd expect with my INotifyPropertyChanged implementation. 数据绑定到NonCollection项目的工作与我的INotifyPropertyChanged实现所期望的一样。

Though you haven't included the code snippet for the Items property, I would guess that the problem is that you are not firing the PropertyChanged event when modifying the value of the property (that is, changing the reference to another object). 尽管您尚未包括Items属性的代码段,但我想问题是您在修改属性值(即更改对另一个对象的引用)时没有触发PropertyChanged事件。 If you want to keep the code that doesn't work, you should implement the Items property like this: 如果要保留无效的代码,则应像这样实现Items属性:

private IEnumerable<Item> items;

public IEnumerable<Item> Items
  {
      get { return this.items; }
      set
      {
          this.items = value;
          // Call OnPropertyChanged whenever the property is updated
          OnPropertyChanged("Items");
      }
  }

  protected void OnPropertyChanged(string name)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
  }

With this implementation, you wouldn't need the Items collection to be an ObservableCollection, but each time you would want to modify it (adding or removing items), you should replace it entirely. 使用此实现,您不需要将Items集合设为ObservableCollection,但是每次您想要对其进行修改(添加或删除项目)时,都应完全替换它。

Of course you could keep the type as ObservableCollection instead of IEnumerable but take into account the overhead that this kind of collection has over others like List or Array. 当然,您可以将类型保留为ObservableCollection而不是IEnumerable,但要考虑到此类集合相对于List或Array等其他类型的开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM