1

我有一个使用 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));
        }
    }

xml:

     <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; 并设置;对于突出显示的项目也不起作用。仍然说成员 highlightItem 无法识别或无法访问

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 我正在尝试创建一个名为 highlightIndex 的依赖属性,它与 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;
        }
    }        
}
}
4

2 回答 2

0

我有点困惑,因为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}" />
于 2013-05-25T04:51:48.223 回答
0
    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();
            }
        }
    }

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

于 2013-05-27T20:55:34.663 回答