我的Application中有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;
}
}发布于 2013-10-23 12:20:58
您更改了歌曲项的属性,但是您已经绑定到包含歌曲项的集合。除非在更改项时引发CollectionChangedEvent,否则不会通知它更新UI。
我想您必须实现像ObservableCollection and Item PropertyChanged这样的通知才能更新UI。
https://stackoverflow.com/questions/19539872
复制相似问题