在 WP7 中绑定数据后更新页面 [英] Updating a Page after Binding Data to it in WP7

查看:16
本文介绍了在 WP7 中绑定数据后更新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有从服务器获取数据的代码,数据被解压缩和解析,然后文件的名称以及与它们相关的图标应该显示在 UI 上,我们正在使用列表框并尝试绑定将这 2 个元素添加到列表框,数据正在绑定,但我们无法在绑定后更新页面.`

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. UIManager 类设置为 XAML 控件的 dataContext.
  2. UIManager 必须实现 INotifyPropertyChanged 才能通知您绑定到的集合的 UI 已更改(在您的案例中添加了一些项目).

  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).

最重要的 - 使用输出窗口来定位所有 XAML 绑定问题.

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

类 UIManager:INotifyPropertyChaged{

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

}

这篇关于在 WP7 中绑定数据后更新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆