我已经创建了一个运行在Windows Phone 8.1平台的应用程序。
我正在使用ListView控件。
我希望交替设置每个背景行的颜色。
在搜索后,我找到了这个链接先前的回答。
但是这会在标记中产生错误。首先没有'AlternationCount'属性。 我假设这是因为它不是SilverLight而是RT?
如果有人能够给我发送一个链接,因为我正在努力寻找一个简单的示例。更好的是,提供一个简单的代码示例将不胜感激。
我已经创建了一个运行在Windows Phone 8.1平台的应用程序。
我正在使用ListView控件。
我希望交替设置每个背景行的颜色。
在搜索后,我找到了这个链接先前的回答。
但是这会在标记中产生错误。首先没有'AlternationCount'属性。 我假设这是因为它不是SilverLight而是RT?
如果有人能够给我发送一个链接,因为我正在努力寻找一个简单的示例。更好的是,提供一个简单的代码示例将不胜感激。
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
不会被调用),而添加项目则会。但在您的情况下,这应该已经足够了。
public 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),
typeof(AlternateConverter), 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, Type targetType, object parameter, 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>
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);
}
}
只需将ListView的ContainerContentChanging事件挂钩即可。我不知道当您重新排序列表时它是否有效,但在正常情况下,它非常有效。
您甚至可以实现自己的ListView,以便尽可能多地使用它。通过正确的属性设置,您还可以在xaml文件中进行编辑。例如,分配#FFFF0000(ARGB)。
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);
}
}
}
<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);
}
}
感谢你们两位的回复 - 非常感谢。我有另一个解决方案想提出来,请大家在这里发表评论。
lvwPremises.Items.Clear();
bool toggle = false;
foreach (Premise premise in Shared.Premises)
{
ListViewItem item = new ListViewItem();
item.Content = premise.PremiseName;
if (toggle)
{
item.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 223, 240, 216));
}
else
{
item.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 208, 233, 198));
}
lvwPremises.Items.Add(item);
toggle = !toggle;
}
编辑 - 由Romasz
如果您愿意,您可以随时在代码中进行修改,但这样您的解决方案就不太通用了,必须在更改集合时始终运行,并可能存在其他问题。我将此评论作为对您答案的编辑,因此上面的代码可能可以简化,看起来像这样(在答案中更好看):
private void ColorBackgrounds(ListView list, IList<SolidColorBrush> backgrounds)
{
for (int i = 0; i < list.Items.Count; i++)
(list.ContainerFromIndex(i) as ListViewItem).Background = backgrounds[i % backgrounds.Count];
}
ListViewItem
。 - Justin XLListView
数据绑定流程 - 您定义一个ObservableCollection<Premise>
,并将ListView
的ItemsSource
绑定到它。然后在ListView
的ItemTemplate
中,一个TextBlock
将其Text
属性绑定到PremiseName
。ListViewItem
是包装ItemTemplate
的东西。通常情况下,您只会在xaml中更新ListViewItem
的样式,以便具有不同的外观和感觉。希望这可以稍微澄清一下! - Justin XL