我有一个ObservableCollection
,它是实现INotifyPropertyChanged
接口的视图模型的集合......
public class FeedsViewModel : ObservableCollection<FeedViewModel>
{
}
(我没有包含代码,FeedViewModel
但它非常标准INotifyPropertyChanged
并且在独立测试时绑定)。
我在控件中使用FeedsViewModel
as 的一个实例...ItemsSource
LongListSelector
<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>
但是,当我在运行时向 ObservableCollection 添加一些内容时,UI 不会随着更改而更新......
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
并检查集合表明CollectionChanged
集合事件没有处理程序(为空)。
如何让 LongListSelector 从 ObservableCollection 更新自身?