0

我有ListBoxApplication设计的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;
            }
    }
4

1 回答 1

1

您更改了 Song-Item 的属性,但是您已经绑定到包含 Song-Item 的 Collection。不会通知更新 UI,除非您CollectionChangedEvent在更改项目时引发 a。

我猜你必须实现像ObservableCollection 和 Item PropertyChanged这样的通知才能更新 UI。

于 2013-10-23T12:20:58.810 回答