Windows Phone 8.1中ListView行的交替颜色
我已经创建了一个Windows Phone 8.1运行时应用程序 我正在使用ListView控件 我想交替每个背景行的颜色 搜索后我找到了这个链接 但这会在标记中产生错误。首先,没有“AlternationCount”属性。我假设这是因为它不是SilverLight而是RTWindows Phone 8.1中ListView行的交替颜色,listview,windows-runtime,windows-phone-8.1,listviewitem,Listview,Windows Runtime,Windows Phone 8.1,Listviewitem,我已经创建了一个Windows Phone 8.1运行时应用程序 我正在使用ListView控件 我想交替每个背景行的颜色 搜索后我找到了这个链接 但这会在标记中产生错误。首先,没有“AlternationCount”属性。我假设这是因为它不是SilverLight而是RT 如果有人能给我一个链接,因为我正在努力寻找一个简单的例子。最好是一个简单的代码示例。WPF是唯一支持“AlternationCount”的框架——Windows Phone、Silverlight和RT都没有 您可能会发现,
如果有人能给我一个链接,因为我正在努力寻找一个简单的例子。最好是一个简单的代码示例。WPF是唯一支持“AlternationCount”的框架——Windows Phone、Silverlight和RT都没有 您可能会发现,最简单的解决方案就是向行模型中添加“Index”或“IsOdd”属性。您可以只绑定到该属性,使用转换器根据索引返回适当的笔刷/颜色 一种更简单的方法是绑定到一个转换器,该转换器使用静态变量跟踪索引,如下所示。但是,如果您使用项目虚拟化(除非只有几个项目,否则您可能会这样做),那么这种方法会遇到小问题:延迟创建UI元素会导致索引分配顺序混乱,最终导致连续行显示相同的颜色
<Border Background="{Binding Converter={StaticResource AlternatingIndexConverter}}">
public class AlternatingIndexConverter : IValueConverter
{
private static int _index;
public Brush Even { get; set; }
public Brush Odd { get; set; }
public object Convert(...)
{
return (_index++ % 2 == 0 ? Even : Odd);
}
}
公共类AlternatingIndexConverter:IValueConverter
{
私有静态int_索引;
公共笔刷偶数{get;set;}
公共笔刷奇数{get;set;}
公共对象转换(…)
{
返回(_index++%2==0?偶数:奇数);
}
}
我的建议是使用一个具有附加依赖属性的转换器类。初始化转换器时,定义它将引用的项目集合以及背景的备用笔刷列表。例如,它可以如下所示:
公共类AlternateConverter:DependencyObject,IValueConverter
{
公共列表交替使用
{
获取{return(List)GetValue(alternatebrusheproperty);}
set{SetValue(alternatebrusheproperty,value);}
}
公共静态只读从属属性AlternateBrushProperty=
从属财产登记簿(“替代登记簿”,类型(列表),
typeof(AlternateConverter)、新属性元数据(new List());
公共对象列表
{
获取{返回GetValue(CurrentListProperty);}
set{SetValue(CurrentListProperty,value);}
}
公共静态只读DependencyProperty CurrentListProperty=
DependencyProperty.Register(“当前列表”,类型为(对象),
typeof(AlternateConverter),新属性元数据(null);
公共对象转换(对象值、类型targetType、对象参数、字符串语言)
{返回AlternateBrushes[(当前列表为IList.IndexOf(value)%AlternateBrushes.Count];}
公共对象转换回(对象值、类型targetType、对象参数、字符串语言)
{抛出新的NotImplementedException();}
}
定义并创建备用笔刷列表后:
//数据上下文中的某个地方
私有列表笔刷=新列表{new SolidColorBrush(Colors.Red)、new SolidColorBrush(Colors.Blue)};
公共列表笔刷{获取{返回笔刷;}}
您可以这样使用它:
此解决方案应该可以工作,但在使用IList的值类型时可能会出现问题。这里也应该没有延迟创建的问题,因为它直接从列表中检索索引。感谢您的回复,非常感谢。我想提出另一个解决方案,我把它贴在这里供人们评论
lvwPremises.Items.Clear();
bool-toggle=false;
foreach(共享的前提中的前提)
{
ListViewItem=新建ListViewItem();
item.Content=premise.PremiseName;
如果(切换)
{
item.Background=newsolidColorBrush(Windows.UI.Color.FromArgb(255、223、240、216));
}
其他的
{
item.Background=newsolidColorBrush(Windows.UI.Color.FromArgb(255、208、233、198));
}
lvwPremises.Items.Add(item);
切换=!切换;
}
编辑-由Romasz编写 如果您愿意,您可以始终在代码中运行,但是您的解决方案不是那么通用,在更改集合时必须始终运行,并且可能存在其他问题。我将此评论作为对您答案的编辑,因此上面的代码可能会被简化,并且看起来可能是这样的(在答案中看起来更漂亮):
private void ColorBackgrounds(列表视图列表,IList背景)
{
对于(int i=0;i
我知道这个问题已经有了一些很好的答案,但我只想再提出一个想法,我认为这有点难以实现,但更易于使用
此解决方案需要来自列表视图
的ItemContainerStyleSelector
的帮助,以及来自行为SDK(XAML)的行为
基本上,我创建的这个AlternatingColorItemContainerStyleSelector
行为允许您指定两种SolidColorBrush
颜色。它封装了使用两种不同的样式创建ItemContainerStyleSelector
的逻辑,并将相应的SolidColorBrush
分配给每个样式
一旦你有了合适的行为,使用它是非常简单的-我只需要将它拖放到Expression Blend中的列表视图
,然后指定两种颜色就可以了
这就是行为
namespace Behaviors
{
public class AlternatingColorItemContainerStyleSelector : StyleSelector
{
private Style _oddStyle = new Style { TargetType = typeof(ListViewItem) }, _evenStyle = new Style { TargetType = typeof(ListViewItem) };
public Style OddStyle { get { return _oddStyle; } }
public Style EvenStyle { get { return _evenStyle; } }
protected override Style SelectStyleCore(object item, DependencyObject container)
{
var listViewItem = (ListViewItem)container;
var listView = GetParent<ListView>(listViewItem);
var index = listView.IndexFromContainer(listViewItem);
if (index % 2 == 0)
{
return this.EvenStyle;
}
else
{
return this.OddStyle;
}
}
public static T GetParent<T>(DependencyObject child) where T : DependencyObject
{
while (!(child is T))
{
child = VisualTreeHelper.GetParent(child);
}
return (T)child;
}
}
public class ListViewAlternatingColorBehavior : DependencyObject, IBehavior
{
public DependencyObject AssociatedObject { get; set; }
public Style SharedItemContainerStyle { get; set; }
#region colors dp
public SolidColorBrush OddBrush
{
get { return (SolidColorBrush)GetValue(OddBrushProperty); }
set { SetValue(OddBrushProperty, value); }
}
public static readonly DependencyProperty OddBrushProperty =
DependencyProperty.Register("OddBrush", typeof(SolidColorBrush), typeof(ListViewAlternatingColorBehavior), new PropertyMetadata(null));
public SolidColorBrush EvenBrush
{
get { return (SolidColorBrush)GetValue(EvenBrushProperty); }
set { SetValue(EvenBrushProperty, value); }
}
public static readonly DependencyProperty EvenBrushProperty =
DependencyProperty.Register("EvenBrush", typeof(SolidColorBrush), typeof(ListViewAlternatingColorBehavior), new PropertyMetadata(null));
#endregion
public void Attach(DependencyObject associatedObject)
{
this.AssociatedObject = associatedObject;
this.ApplyItemContainerStyleSelectors();
}
private void ApplyItemContainerStyleSelectors()
{
var itemContainerStyleSelector = new AlternatingColorItemContainerStyleSelector();
if (this.SharedItemContainerStyle != null)
{
itemContainerStyleSelector.OddStyle.BasedOn = this.SharedItemContainerStyle;
itemContainerStyleSelector.EvenStyle.BasedOn = this.SharedItemContainerStyle;
}
itemContainerStyleSelector.OddStyle.Setters.Add(new Setter { Property = Control.BackgroundProperty, Value = this.OddBrush });
itemContainerStyleSelector.EvenStyle.Setters.Add(new Setter { Property = Control.BackgroundProperty, Value = this.EvenBrush });
var listView = (ListView)this.AssociatedObject;
listView.ItemContainerStyleSelector = itemContainerStyleSelector;
}
public void Detach()
{
}
}
}
命名空间行为
{
公共类AlternatingColorItemContainerStyleSelector:StyleSelector
{
私有样式_oddStyle=new样式{TargetType=typeof(ListViewItem)},_evenStyle=new样式{TargetType=typeof(ListViewItem)}
private void ListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.ItemIndex%2 != 0)
{
args.ItemContainer.Background = new SolidColorBrush(Colors.Aqua);
}
else
{
args.ItemContainer.Background = new SolidColorBrush(Colors.White);
}
}
public class BackgroundAlternatingListView : ListView
{
private Color _startColor = Colors.White;
private Color _secondColor = new Color { A = 255, R = 198, G = 198, B = 198 };
public Color StartColor
{
get { return _startColor; }
set { _startColor = value; }
}
public Color SecondColor
{
get { return _secondColor; }
set { _secondColor = value; }
}
public BackgroundAlternatingListView()
{
ContainerContentChanging += BackgroundAlternatingListView_ContainerContentChanging;
}
void BackgroundAlternatingListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.ItemIndex % 2 != 0)
{
args.ItemContainer.Background = new SolidColorBrush(_secondColor);
}
else
{
args.ItemContainer.Background = new SolidColorBrush(_startColor);
}
}
}