2

I'm trying to highlight the playing song from a listview, and running into some trouble. Often, lvicurrent is null. Here is my code...

    private void highlightItem(String focused)
    {
        //songlistView.ItemContainerGenerator.ItemsChanged????;


        ListViewItem lvicurrent = new ListViewItem();

        if (lviPrevious != null)
        {
            lviPrevious.Background = new SolidColorBrush();
        }

        if (focused.Equals("songListview"))
        {
            lvicurrent = songlistView.ItemContainerGenerator.ContainerFromIndex(songIndex) as ListViewItem;
            lviPrevious = lvicurrent;
            lvicurrent.Background = new SolidColorBrush(Colors.LightGreen);
        }
        else if (focused.Equals("playListview"))
        {
            lvicurrent = playlistView.ItemContainerGenerator.ContainerFromIndex(songIndex) as ListViewItem;
            lviPrevious = lvicurrent;
            lvicurrent.Background = new SolidColorBrush(Colors.LightGreen);
        }
    }

    private void StatusChangedEventHandler(object sender, EventArgs e)
    {

    }

Only thing I could find on the internet is wait for the status to be finished, but there is no status property in winrt as far as I can tell.

My other problem is that more than 1 song is being displayed in green. I don't know why this is, maybe because it's a virtualizing stackpanel... but then one would think it would not affect songs not currently visible in the stackpanel. This is the very last thing I need to fix before I publish the app.

4

1 回答 1

0

如果 ItemConatinerGenerator.ContainerFromIndex() 返回 null,则表示该索引处的容器尚未生成(即项目的容器仍处于虚拟化状态)。

如果您有一个简短的集合,您可能会使用 StackPanel 作为 ListView 的 ItemsPanel 而不是 VirtualizingStackPanel(默认值)。使用 StackPanel,容器将不会被虚拟化。如果您有更长的集合,虚拟化会更好地提高您的应用程序的性能。

您的场景似乎需要您“突出显示”某个项目索引的容器。如果您要添加“突出显示”效果,StyleSelector 听起来很有希望。您也可以通过继承 ListView/ListViewItem 来实现这一点。您是否考虑过为您的场景重新使用选择的概念?ListView 继承自 Selector 并支持一些 SelectionModes。您可以使用“选择”视觉状态来实现“突出显示”效果。

于 2013-05-26T17:36:45.240 回答