我ListBox
中的Application
ItemTemplateSelector
设有TemplateSelector templateSelector = new TemplateSelector();
templateSelector.SongTemplate = Resources["Template"] as DataTemplate;
templateSelector.SongTemplateCached = Resources["TemplateCached"] as DataTemplate;
HistoryList.ItemTemplateSelector = templateSelector;
:
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");
}
他们之间的不同之处在于,如果这首歌是下载或不下载的话。
这是列表框的ViewModel:
UpdateCache
当歌曲完成下载时,会调用<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>
方法,但ListBox不会更新(将设计从SongTemplate模式更改为SongTemplateCached)
我如何解决它?
XAML:
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;
}
}
选择器:
{{1}}
答案 0 :(得分:1)
您更改了Song-Item的属性,但是您已经绑定了包含Song-Item的Collection。除非您在更改项目时提出CollectionChangedEvent
,否则不会通知它更新UI。
我猜您必须实施ObservableCollection and Item PropertyChanged之类的通知才能更新用户界面。