当变量发生变化时,如何更新列表视图

本文关键字:更新 视图 列表 何更新 变量 变化 | 更新日期: 2023-09-27 18:15:31

我有一个使用StyleSelector的条件样式,这样当程序加载时,它会将播放歌曲的颜色更改为绿色。然而,当songIndex静态变量被更改时,我不知道如何更新它。我尝试使用INotifyPropertyChanged接口,但不确定如何正确使用它,也不确定应该绑定什么。。。。

public class HighlightStyleSelector : StyleSelector, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected override Style SelectStyleCore(object item, DependencyObject container)
    {
        //List<myMediaInterface> mediaList = new List<myMediaInterface>();
        if (item == MainPage.mediaList[MainPage.songIndex])
        {
            Style style = new Style(typeof(ListViewItem));
            style.Setters.Add(new Setter(ListViewItem.BackgroundProperty, new SolidColorBrush(Colors.LightGreen)));
            return style;
        }
        else
        {
            var style = Application.Current.Resources["ListViewItemStyle1"] as Style;
            return null;
        }
    }

    public int songIndex
    {
        get { return MainPage.songIndex; }
        set
        {
            songIndex = MainPage.songIndex;
            OnPropertyChanged(songIndex.ToString());
        }
    }
    protected void OnPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Xaml:

     <ListView x:Name="songlistView" SelectionMode="Extended" DoubleTapped="songlistView_DoubleTapped" HorizontalContentAlignment="Stretch" BorderThickness="1" BorderBrush="#FF616161" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Enabled" ManipulationMode="None" UseLayoutRounding="False" VerticalContentAlignment="Stretch" Margin="2,150,0,558" Tapped="songlistView_Tapped" FontSize="14"  ItemContainerStyleSelector="{StaticResource HighlightStyleSelector}" ItemsSource="{Binding MainPage.mediaList}">

这是自定义列表视图的代码

    namespace HelloWorld
    {
        public class MyListView : Control
        {
            public int highlightedItem;
            public MyListView()
            {
                this.DefaultStyleKey = typeof(MyListView);
            }
        }
}

如果我使用get;和集合;对于高亮项目也不起作用。仍然表示成员highlightedItem未被识别或无法访问

编辑5/25这现在在MainPage.xaml.cs 中

public int songIndex
    {
        get
        {
            return songIndex;
        }
        set
        {
            songIndex = value;
            OnPropertyChanged("songIndex");
        }
    }

^^不确定这是否应该与我的字段声明一起使用?

    public void OnPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

这是我在MainPage.xaml 上的代码

        <ListView x:Name="songlistView" SelectedIndex="{Binding songIndex}" SelectionMode="Extended" DoubleTapped="songlistView_DoubleTapped" HorizontalContentAlignment="Stretch" BorderThickness="1" BorderBrush="#FF616161" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Enabled" ManipulationMode="None" UseLayoutRounding="False" VerticalContentAlignment="Stretch" Margin="2,150,0,558" Tapped="songlistView_Tapped" FontSize="14"  ItemsSource="{Binding MainPage.mediaList}"><!--ItemContainerStyleSelector="{StaticResource HighlightStyleSelector}"-->
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
        </ListView>

这是我的代码5/26我正在尝试创建一个名为highlightedIndex的dependencyproperty,它与selectedIndex完全相同,只是它是独立的。

public class MyListView : ListView
{
    public int highlightedIndex
    {
        get { return (int)GetValue(HighlightedProperty); }
        set
        {
            SetValue(HighlightedProperty, value);
        }
    }
    public static readonly DependencyProperty HighlightedProperty = DependencyProperty.Register("HighlightedProperty", typeof(int), typeof(MyListView), new PropertyMetadata(0));
}

namespace HelloWorld
{
    public class HighlightStyleSelector : StyleSelector
{
    protected override Style SelectStyleCore(object item, DependencyObject container)
    {
        if (item == MainPage.mediaList[MainPage.songIndex])
        {
            var style = Application.Current.Resources["ListViewItemHighlighted"] as Style;
            Setter setter = new Setter(ListViewItem.BackgroundProperty, new SolidColorBrush(Colors.LightGreen));
            //Style style = new Style(typeof(ListViewItem));
            style.Setters.Add(setter);
            return style;
        }
        else
        {
            var style = Application.Current.Resources["ListViewItemStyle1"] as Style;
            return style;
        }
    }        
}
}

当变量发生变化时,如何更新列表视图

我有点困惑,因为MainPage上的静态属性似乎是将songIndex分配给并将mediaList绑定给的。查看这些代码也会很有帮助。

不过,您需要修复您的属性(假设OnPropertyChanged实现正确(:

public int songIndex
{
    get { return MainPage.songIndex; }
    set
    {
        // set the assigned value to property backing field
        MainPage.songIndex = value;
        // you need to notify with the name of the property as the argument
        OnPropertyChanged("songIndex");
    }
}

然后,您可以像其他任何属性一样绑定到此属性,唯一的区别是当控件的值更改时会通知它:

<ListView SelectedIndex="{Binding songIndex}" />
    public static readonly DependencyProperty HighlightedProperty = DependencyProperty.Register("highlightedIndex", typeof(int), typeof(MyListView), new PropertyMetadata(null, propertyChanged));

    private static void propertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        int newValue = (int)e.NewValue;
        ListView lv = (ListView)d;
        foreach (ListViewItem lvi in lv.Items)
        {
            if (lv.Items.IndexOf(lvi) == newValue)
            {
                lvi.Background = new SolidColorBrush(Colors.LightGreen);
            }
            else
            {
                lvi.Background = new SolidColorBrush();
            }
        }
    }

不需要样式选择器或任何绑定