为什么不';将项目添加到我的DataBound集合时,我的视图不会更新
本文关键字:我的 集合 视图 更新 DataBound 为什么不 项目 添加 | 更新日期: 2023-09-27 18:29:48
我的简化代码如下:
型号
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");
}
}
}
ViewModel
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>
在Playlist
中添加或删除Track
时,为什么我的视图不更新?
我发现了问题:我没有正确删除或添加曲目。我在模型上修改了它们,而不是在我的Collection属性上。
一旦我修改了我的属性,事件就通过了,我的视图也更新了。