Updating a Page after Binding Data to it in WP7

南笙酒味 提交于 2020-01-07 06:37:47

问题


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

    }



来源:https://stackoverflow.com/questions/7526650/updating-a-page-after-binding-data-to-it-in-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!