我已经创建了一个
Windows Phone 8.1运行时应用程序.
我正在使用ListView控件.
我想交替每个背景行的颜色.
搜索后我发现这个链接a previous answer.
但是这会给出标记错误.一方面没有’AlternationCount’属性.我假设这是因为它不是SilverLight而是RT?
我的建议是使用带有附加DependencyProperties的Converter类.当您初始化转换器时,您可以定义要引用的项目集合以及背景的备用画笔列表.它可以像这样寻找例子:
原文链接:https://www.f2er.com/windows/364223.htmlpublic class AlternateConverter : DependencyObject,IValueConverter { public List<SolidColorBrush> AlternateBrushes { get { return (List<SolidColorBrush>)GetValue(AlternateBrushesProperty); } set { SetValue(AlternateBrushesProperty,value); } } public static readonly DependencyProperty AlternateBrushesProperty = DependencyProperty.Register("AlternateBrushes",typeof(List<SolidColorBrush>),typeof(AlternateConverter),new PropertyMetadata(new List<SolidColorBrush>())); public object CurrentList { get { return GetValue(CurrentListProperty); } set { SetValue(CurrentListProperty,value); } } public static readonly DependencyProperty CurrentListProperty = DependencyProperty.Register("CurrentList",typeof(object),new PropertyMetadata(null)); public object Convert(object value,Type targetType,object parameter,string language) { return AlternateBrushes[(CurrentList as IList).IndexOf(value) % AlternateBrushes.Count]; } public object ConvertBack(object value,string language) { throw new NotImplementedException(); } }
一旦定义并创建备用画笔列表:
// somewhere in your DataContext private List<SolidColorBrush> brushes = new List<SolidColorBrush> { new SolidColorBrush(Colors.Red),new SolidColorBrush(Colors.Blue) }; public List<SolidColorBrush> Brushes { get { return brushes; } }
你可以这样使用:
<ListView x:Name="myList" ItemsSource={Binding MyItems}> <ListView.Resources> <local:AlternateConverter CurrentList="{Binding ElementName=myList,Path=ItemsSource}" AlternateBrushes="{Binding Brushes}" x:Key="AlternateConverter"/> </ListView.Resources> <ListView.ItemTemplate> <DataTemplate> <Border Background="{Binding Converter={StaticResource AlternateConverter}}"> <!-- your itemtemplate --> </Border> </DataTemplate> </ListView.ItemTemplate> </ListView>
这个解决方案应该是有效的,虽然它有价值类型的IList可能有问题.这里也不应该是延迟创建的问题,因为它直接从列表中搜索索引.