gpt4 book ai didi

listview - Windows Phone 8.1 中 ListView 中行的交替颜色

转载 作者:行者123 更新时间:2023-12-02 13:47:07 26 4
gpt4 key购买 nike

我创建了一个 Windows Phone 8.1 运行时应用程序。

我正在使用ListView控件。

我想交替每个背景行的颜色。

搜索后我发现了这个链接a previous answer .

但这会导致标记错误。一方面,没有“AlternationCount”属性。我假设这是因为它不是 SilverLight 而是 RT?

如果有人可以给我发送一个链接,因为我正在努力寻找一个简单的例子。如果有简单的代码示例就更好了。

最佳答案

我知道这个问题已经有一些很好的答案,但我只想提出一个想法,我认为它实现起来有点困难,但使用起来更容易。

此解决方案需要 ListView 的帮助的ItemContainerStyleSelector和一个 Behavior来自行为 SDK (XAML)

基本上,这个AlternatingColorItemContainerStyleSelector我创建的行为允许您指定两个 SolidColorBrush颜色。它封装了创建 ItemContainerStyleSelector 的逻辑有两个不同的Style s 以及分配相应的 SolidColorBrush给每个Style .

一旦您完成了该行为,使用它就非常简单 - 我只需将其拖放到 ListView 上即可。在 Expression Blend 中指定两种颜色即可!

enter image description here

这是行为。

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()
{
}
}
}

需要注意的一件事是,删除项目不会更新所有其他项目的颜色(仅仅是因为其他项目的 SelectStyleCore 不会被调用),添加项目则会更新。但就你而言,这应该足够了。

关于listview - Windows Phone 8.1 中 ListView 中行的交替颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27607886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com