日期:2014-05-18  浏览次数:21277 次

WPF的listview的行实现斑马线
我想用WPF的listview控件实现斑马线的功能(比如奇数行是一个颜色偶数行又是一个颜色)

------解决方案--------------------
我还以为真的要画斑马线……
------解决方案--------------------
Microsoft.Windows.Controls.DataGrid dg=new Microsoft.Windows.Controls.DataGrid();
dg.AlternatingRowBackground="Gray" //背景斑马线里的灰色
dg.AlternationCount="2" //每2行一行灰色的
------解决方案--------------------
探讨
Microsoft.Windows.Controls.DataGrid dg=new Microsoft.Windows.Controls.DataGrid();
dg.AlternatingRowBackground="Gray"  //背景斑马线里的灰色
dg.AlternationCount="2"  //每2行一行灰色的

------解决方案--------------------
for (int i = 0; i < 10; i++) 

WrapPanel panel = new WrapPanel();
TextBlock tb = new TextBlock(); 
tb.Text = i.ToString(); 
tb.Background = Brushes.Blue; 
tb.Width = 20; 
panel.Children.Add(tb); 
this.listView1.Items.Add(panel); 
}

先做一个panel,然后控制panel背景色,再将panel放进listView
------解决方案--------------------
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/wpf_conceptual/html/955cc6b9-53eb-4026-ae93-0f875eb5558c.htm
微软的帮助文档
各种WPF交替颜色方法
此示例演示您可用于使 ListView 中各行的 Background 颜色产生交替效果的三种方法。

示例
以下各节提供了三种方法,用于创建各行的 Background 颜色具有交替效果的 ListView。该示例还论述用于在添加或移除行时更新视图的方法。

方法 1:定义使用 IValueConverter 来使背景色产生交替效果的样式 

下面的示例显示如何为将 Background 属性的值绑定到 IValueConverter 的 ListViewItem 控件定义 Style。

XAML 复制代码 
<Style x:Key="myItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background">
<Setter.Value>
<Binding RelativeSource="{RelativeSource Self}" 
Converter="{StaticResource myConverter}"/>
</Setter.Value>
</Setter>
</Style>


 

下面的示例为 IValueConverter 定义 ResourceKey。

XAML 复制代码 
<namespc:BackgroundConverter x:Key="myConverter"/>


 

下面的示例显示依据行索引设置 Background 属性的 IValueConverter 的定义。

C# 复制代码 
public sealed class BackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, 
CultureInfo culture)
{
ListViewItem item = (ListViewItem)value;
ListView listView = 
ItemsControl.ItemsControlFromItemContainer(item) as ListView;
// Get the index of a ListViewItem
int index = 
listView.ItemContainerGenerator.IndexFromContainer(item);

if (index % 2 == 0)
{
return Brushes.LightBlue;
}
else
{
return Brushes.Beige;
}
}


 

下面的示例演示如何定义使用 Style 作为其 ItemContainerStyle 以便提供所需布局的 ListView。

XAML 复制代码 
<ListView Name="theListView" 
ItemsSource="{Binding Source={StaticResource EmployeeData}, 
XPath=Employee}"
ItemContainerStyle="{StaticResource myItemStyle}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding XPath=FirstName}" 
Header="First Name" Width="120"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath=LastName}" 
Header="Last Name" Width="120"/>