Updating a Page after Binding Data to it in WP7

2019-09-16 07:05发布

问题:

we have code that is getting data from the server,the data is unzipped and parsed then the name of the files along with the icons related to them are supposed to be displayed on the UI, we are using a listbox and trying bind these 2 elements to the listbox, the data is getting bound but we are not able to update the page after binding.`

</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
     <Image Source="{Binding Icon, Mode=OneWay}" Grid.Column="0" HorizontalAlignment="Center" Grid.Row="1"/>
<TextBlock Padding="10" Text="{Binding Widget, Mode=OneWay}" FontSize="20" Grid.Row="2" Grid.RowSpan="2" TextWrapping="Wrap"  Grid.ColumnSpan="2" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>`   

回答1:

Make sure that:

  1. UIManager class is set as a dataContext for your XAML control.
  2. UIManager has to implement INotifyPropertyChanged in order to notify the UI that the collection you are binding to has changed (has some items added to it in your case).

  3. Most important - use Output windows to locate all XAML binding issues.

    class UIManager: INotifyPropertyChaged {

    private ObservableCollection<ItemsList> _displayItem;
    
        public ObservableCollection<ItemsList> DisplayItem
        {
           get
           {
              return _displayItem;
           }
           set
           {
              if(value != _displayItem)
              {
                 _displayItem=value;
                 NotifyPropertyChanged("DisplayItem");
              }
           }
    
        public UIManager()
        {
           DisplayItem = new ObservableCollection<ItemsList>();
           DisplayCat(DataManager.getInstance().displayName, DataManager.getInstance().icons);
        }
    
        public void DisplayCat(string[] DisplayNames, BitmapImage[] Icon)
        {
                ObservableCollection<ItemsList> tmpColl = new ObservableCollection<ItemsList>();        
    
        for (int i = 0; i < DataManager.getInstance().count; i++)
            {
                    tmpColl.Add(new ItemsList { Widget = DisplayNames[i], Icon = Icon[i] });
            }
        DisplayItem = tmpColl;
        }
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    private void NotifyPropertyChanged(String info)
    {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
    }
    

    }