Xaml Silverlight列表框不刷新列表

Xaml Silverlight列表框不刷新列表,xaml,silverlight-4.0,listbox,odata,Xaml,Silverlight 4.0,Listbox,Odata,我试图加载一个列表框,其中包含从ODataWeb服务中提取的项目列表。数据提取工作正常,我正在获取项目列表。列表框实际上可以工作并显示项目,只是不是每次我启动应用程序时。。。由于异步拉取数据的要求,有时listbox会在数据返回之前加载。当它这样做时,我会给它一个列表,其中有一个“空”项,以指示数据仍在加载。几秒钟后,数据加载完毕,我为列表引发PropertyChanged事件。当我检查列表是否包含正确的项时,列表属性中的断点将触发。但是列表框不显示新项目,只显示旧的“空”项目。对我来说,xam

我试图加载一个列表框,其中包含从ODataWeb服务中提取的项目列表。数据提取工作正常,我正在获取项目列表。列表框实际上可以工作并显示项目,只是不是每次我启动应用程序时。。。由于异步拉取数据的要求,有时listbox会在数据返回之前加载。当它这样做时,我会给它一个列表,其中有一个“空”项,以指示数据仍在加载。几秒钟后,数据加载完毕,我为列表引发PropertyChanged事件。当我检查列表是否包含正确的项时,列表属性中的断点将触发。但是列表框不显示新项目,只显示旧的“空”项目。对我来说,xaml显然在请求列表,但却没有刷新新项目的布局,这似乎异常奇怪

首先是初始化ViewModel的代码。ModelReferenceMap实现INotifyPropertyChanged,因此在OnPropertyChangedAras时应该更新视图;这将触发从属性获取列表,但不会更新视图

    public ModelReferenceMap(Uri serviceURI)
    {
        // Try initialising these lists to a non null but empty list
        // in the hope it will stop the lists breaking when the service
        // is a little bit slow...
        areas = new List<ModelReferenceItem> { new ModelReferenceItem(null) };
        // This is a ServiceReference entity context which will retrieve the data from the OData service
        context = new LiveEntities(serviceURI);
        // SendingRequest adds credentials for the web service
        context.SendingRequest += context_SendingRequest;
        // The query to retrieve the items
        var areaQuery = from i in context.MigrationItems where i.FusionPTID == 0 && i.Type == "AreaType" orderby i.Name select i;
        // On completion this asynccallback is called
        AsyncCallback ac = iasyncResult =>
        {
            // Populates the List with the data items
            areas = (from i in ((DataServiceQuery<MigrationItem>) areaQuery).EndExecute(iasyncResult)
                    select new ModelReferenceItem(i)).ToList();
            foreach (ModelReferenceItem area in areas)
            {
                if (selectedArea == null)
                    selectedArea = area;
                area.PropertyChanged += referenceItem_PropertyChanged;
            }
            // The Xaml Listbox has its ItemsSource bound to the Areas property. This should trigger a refresh of the listbox contents shouldn't it?
            OnPropertyChanged("Areas");
            OnPropertyChanged("SelectedArea");
        };
        // Start the query
        ((DataServiceQuery<MigrationItem>)areaQuery).BeginExecute(ac, null);
    }
现在是XAML。请注意,listbox的DataContext是我的主ViewModel上的ReferenceMap属性,它公开ModelReferenceMap的单个实例。然后我将ItemsSource绑定到区域

      <ListBox Grid.Row="0" DataContext="{Binding ReferenceMap}" ItemsSource="{Binding Areas}" SelectedItem="{Binding SelectedArea, Mode=TwoWay}" HorizontalAlignment="Stretch" Margin="3" Name="listBoxFusionAreas" VerticalAlignment="Stretch">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="{Binding CompleteStatusColour}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock FontSize="12" Text="{Binding Name}" />
                        <TextBlock Grid.Column="1" FontSize="12" Text="{Binding Count}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
Areas属性正在正确触发,指示绑定正在工作。仅在检索到服务数据后才请求区域,即仅在列表正常工作时。但是,如果在服务数据返回ie之前触发了Areas属性,则在OnPropertyChangedAreas过程中,它会再次触发带有单个“空”项的ie;调用完整的项目集,只是这次列表仍然只显示原始的“空”项目


我做错了什么?

无论何时绑定到ViewModel中的集合,都需要确保集合中的项目是否会更改??在您的情况下,您需要实现

ObservableCollection<ModelReferenceItem> areas ;
而不是

List<ModelReferenceItem> area;

ObservableCollection实现INoifyCollectionChanged事件,该事件通知您的视图集合中的更改添加/删除

我将给出一个快照,但是当服务最终返回数据时,整个列表将被替换。与其说是添加和删除单个项目,不如说是添加和删除单个项目。如果它不能立即工作,我将尝试使用clear和addrange,而不是替换现有的列表…似乎已经做到了这一点。区域现在是可观测的集合。当数据返回时,我调用区域。清除;然后是区域。添加;对于处理的单个项目,即添加事件处理程序。为快速反应干杯。他妈的肯定几年前我碰到过这个。。。但我想,如果你在iOS上开发18个月,就会发生这种情况: