我有一个使用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;并设定;对于高亮显示的项目也不起作用。仍然说成员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 我正在尝试创建一个名为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;
}
}
}
}
答案 0 :(得分: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}" />
答案 1 :(得分: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();
}
}
}
不需要styleselector或任何绑定