绑定到ObservableCollection的LongListSelector不会更新

时间:2013-01-22 05:54:30

标签: wpf data-binding windows-phone-8 observablecollection longlistselector

我有一个ObservableCollection,它是一个实现INotifyPropertyChanged界面的View Models的集合......

public class FeedsViewModel : ObservableCollection<FeedViewModel>
{
}

(我没有包含FeedViewModel的代码,但它非常标准INotifyPropertyChanged并且在独立测试时绑定。

我在FeedsViewModel控件中使用ItemsSource的{​​{1}}个实例...

LongListSelector

但是,当我在运行时向ObservableCollection添加内容时,UI不会随更改而更新......

<UserControl.Resources>
    <viewModel:FeedStatusFeedbackConverter x:Key="ProgressStatusColorConverter"/>
    <viewModel:FeedStatusFeedbackConverter x:Key="TitleStatusColorConverter"/>
    <viewModel:FeedsViewModel x:Key="FeedsViewModel"/>
</UserControl.Resources>

<phone:LongListSelector ItemsSource="{Binding Source={StaticResource FeedsViewModel}}">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>

            <Grid x:Name="LayoutRoot" 
                  Background="{StaticResource PhoneChromeBrush}"
                  DataContext="{TemplateBinding}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <Grid Grid.Column="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <TextBlock Text="{Binding Path=Name}" 
                               Grid.Row="0"
                               Style="{StaticResource PhoneTextLargeStyle}"
                               Foreground="{Binding Path=Status, Converter={StaticResource TitleStatusColorConverter}}"/>
                    <TextBlock Text="{Binding Path=Uri}" 
                               Grid.Row="1"
                               Style="{StaticResource PhoneTextNormalStyle}" 
                               TextTrimming="WordEllipsis" />
                    <ProgressBar Grid.Row="2"
                                 Maximum="{Binding Path=ItemCount}"
                                 Value="{Binding Path=ItemProgress}"
                                 Foreground="{Binding Path=Status, Converter={StaticResource ProgressStatusColorConverter}}">
                    </ProgressBar>
                </Grid>

                <Grid Grid.Column="1">
                    <Image Name="Delete" 
                           Source="/Assets/Buttons/delete.png"
                           Width="48"
                           Height="48"
                           Margin="5"
                           MouseLeftButtonDown="Press"
                           MouseLeftButtonUp="Release"/>
                </Grid>
            </Grid>

        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

在将新视图模型添加到private void AddFeedClick(object sender, EventArgs e) { var task = FeedModel.Load("Reddit"); var awaiter = task.GetAwaiter(); awaiter.OnCompleted(() => { var model = awaiter.GetResult(); FeedModel.Save(model); ((ObservableCollection<FeedViewModel>)Resources["FeedsViewModel"]).Add(new FeedViewModel(model)); }); } 并检查集合时命中断点,表明集合事件的ObservableCollection没有处理程序(为空)。

如何让LongListSelector从ObservableCollection更新自己?

2 个答案:

答案 0 :(得分:1)

  1. 尝试将绑定集合作为项目源代码而不是静态绑定(设置List.ItemSource = collection)。如果CollectionChanged为null,则表示没有订阅者更改。
  2. 您也可以尝试同步添加项目。

答案 1 :(得分:1)

我通过将AddFeedClick中的广告素材从(ObservableCollection<FeedViewModel>更改为FeedsViewModel来解决此问题,这是该项目的实际类型。虽然没有发生无效的演员,但在这里肯定有一些模糊的OO功能。我怀疑该集合实际上有两个阴影事件,一个用于ObservableCollection类型,另一个用于扩展类型,但我不确定。