如何在Win RT中使用ObservableCollection数据更新列表框

时间:2015-07-15 10:53:35

标签: c# listview listbox windows-runtime

我正在尝试更新listbox表单ObservableCollection,但不知道我做错了什么,我总是得到以下例外。我是 winRT 的新手。

  

该应用程序调用了一个为a编组的接口   不同的线程。 (HRESULT的例外情况:0x8001010E   (RPC_E_WRONG_THREAD))

<ListBox x:Name="UserList" HorizontalContentAlignment="Stretch" Margin="12,41,12,12">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20" />
                        <ColumnDefinition Width="150" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Title}" Margin="3" Grid.Column="0" />
                    <TextBlock Text="{Binding Description}" Margin="3" Grid.Column="1" />
                    <TextBlock Text="{Binding Title}" Margin="3" Grid.Column="2" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>  

C#代码。

protected  override void OnViewModelCollectionChanged(object sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {

         this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
       {
           UserList.ItemsSource = Items;

       }).AsTask().Wait();


    }

1 个答案:

答案 0 :(得分:0)

更新列表,实现interfaceдляINotifyPropertyChanged

class UserData:  INotifyPropertyChanged
    {

     public event PropertyChangedEventHandler PropertyChanged;

     private string _Title;
     public string Title
       {
        get
            {
                return _Title;
            }
        set
            {
             if (_Title != value)
                {
                  _Title = value;
                  if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Title"));
                    }
                }
            }
        }
    }