- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 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 中指定两种颜色即可!
这是行为。
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/
我想在 scilab 中绘制 limacon,我有这个方程需要处理: 我知道r>0和l>0 当我编译以下代码时,我在第 5 行收到此错误: Inconsistent row/column dimens
我试图更好地了解行和行集在 PeopleCode 中的用途?我读完了PeopleBooks,但仍然觉得我没有很好的理解。我希望对这些与应用程序引擎程序相关的内容有更多的了解。也许通过一个例子可能会有所
我有 4 列的行,每列都有一个标题和一些文本。大多数列都有相似数量的文本,将其列中的按钮向下按以匹配其余列。但是,一列的文本较少,并且没有将按钮向下推得足够远。 有没有办法将按钮对齐到行的底部?我想实
我有这个模型 summary = models.TextField() 但我只想有 4 行和 15 列。 此外,如果我这样做,我是否需要重新安装数据库。 最佳答案 TextField
我想在 iPhone 中创建 SSL 服务器套接字的客户端,但我在 iPhone 中找不到任何 API。我有带密码的有效证书文件 最佳答案 你看过OpenSSL了吗? ? 关于iphone - iPh
For Each cell In sheets(1).Range("A50:A606") For Each cell2 In sheets(2).Range("EX2:ACB2") cell2.
这是我的矩阵 [,1] [,2] M -1 -5 T 8 -4 W -3 9 Th
我有一个全局char *在运行时,重新声明为指向声明为 way 的二维数组的指针。 : char (*A)[N][M] = malloc(sizeof(char[BUF_16][N][M])); 然后
我是一名优秀的程序员,十分优秀!