我的简化代码如下所示:
模型
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
?
我发现了问题:我没有正确删除或添加曲目。我在模型上修改了它们,而不是在我的Collection属性上修改了它们。
修改属性后,事件就会通过,并且视图也会更新。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句