我有ListBox
我Application
设计的ItemTemplateSelector
:
TemplateSelector templateSelector = new TemplateSelector();
templateSelector.SongTemplate = Resources["Template"] as DataTemplate;
templateSelector.SongTemplateCached = Resources["TemplateCached"] as DataTemplate;
HistoryList.ItemTemplateSelector = templateSelector;
它们之间的区别在于歌曲是否下载。
这是列表框的 ViewModel:
ObservableCollection<SongItem> songlist;
public const int MAX_RECENT = 10000;
public HistoryViewModel()
{
NotificationCenterManager.Instance.AddObserver(UpdateCache, AppConst.UPDATECACH);
List<SongItem> tmpArr = SqlLiteManager.CreateInstance().LoadHistoryFromDatabase();
songList = new ObservableCollection<SongItem>(tmpArr);
}
public ObservableCollection<SongItem> SongList
{
get { return songlist; }
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
private void UpdateCache(Notification p_notification)
{
RaisePropertyChanged("SomeName");
}
当歌曲下载完成时,UpdateCache
会调用该方法但 ListBox 不会更新(将设计从 SongTemplate 模式更改为 SongTemplateCached)
我该如何解决?
编辑
XAML:
<ListBox ItemsSource="{Binding Path=VideoList}" Name="HistoryList" Style="{StaticResource myListboxStyle}" BorderThickness="0"
Template="{DynamicResource ListViewNewTemplate}" Margin="-2,0,0,0" MouseDoubleClick="Mouse_Double_Click" ScrollViewer.CanContentScroll="False">
<ListBox.Resources>
<!--Defines a context menu-->
<ContextMenu x:Key="MyElementMenu">
<MenuItem Header="Delete from history" Click="MenuItemDelete_Click"/>
</ContextMenu>
<!--Sets a context menu for each ListBoxItem in the current ListBox-->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ContextMenu" Value="{StaticResource MyElementMenu}"/>
</Style>
</ListBox.Resources>
</ListBox>
选择器:
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
string filePath = ApplicationDataPaths.GetRootDataPath() + "\\www\\CachedContent\\";
string file = video.VideoId + "*.mp4";
string[] listfiles = System.IO.Directory.GetFiles(filePath, file, System.IO.SearchOption.TopDirectoryOnly);
if (listfiles.Length > 0)
{
return VideoTemplateCached;
}
else
{
return VideoTemplate;
}
}