我的简化代码如下所示:
模型
class Playlist : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
private ObservableCollection<Track> _tracks;
public ObservableCollection<Track> Tracks
{
get
{
return _tracks;
}
set
{
_tracks = value;
NotifyPropertyChanged("Tracks");
}
}
}
视图模型
class HubPageViewModel : INotifyPropertyChanged
{
private Playlist _currentPlaylist;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public Playlist CurrentPlaylist
{
get
{
return _currentPlaylist;
}
set
{
_currentPlaylist = value;
NotifyPropertyChanged("CurrentPlaylist");
}
}
public HubPageViewModel()
{
_currentPlaylist = new Playlist();
}
}
查看
public sealed partial class HubPage : Page
{
private HubPageViewModel _hubPageViewModel;
public HubPageViewModel HubPageViewModel
{
get
{
return _hubPageViewModel;
}
}
public HubPage()
{
this.InitializeComponent();
viewModel = new HubPageViewModel();
}
}
XAML
<Page
x:Class="MyProject.HubPage"
DataContext="{Binding HubPageViewModel , RelativeSource={RelativeSource Self}}"
mc:Ignorable="d">
<snip>
<HubSection x:Uid="HubSection5" Header="Now Playing"
DataContext="{Binding CurrentPlaylist}" HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
<DataTemplate>
<ListView
AutomationProperties.AutomationId="ItemListViewSection5"
AutomationProperties.Name="Items In Group"
SelectionMode="None"
IsItemClickEnabled="True"
ItemsSource="{Binding Tracks}"
ItemClick="ItemView_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,27.5" Holding="StackPanel_Holding">
<TextBlock Text="{Binding Title}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</HubSection>
<snip>
为什么在向Track
添加或删除Playlist
时,我的视图未更新?
答案 0 :(得分:0)
我发现了问题:我没有正确删除或添加曲目。 我在模型上修改了它们,而不是我的Collection属性。
一旦我修改了我的属性,事件就会通过,我的视图会更新。