WPF的listview的行实现斑马线

51snail 2010-03-01 02:19:10
我想用WPF的listview控件实现斑马线的功能(比如奇数行是一个颜色偶数行又是一个颜色)
...全文
1626 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingfeng5251 2012-05-04
  • 打赏
  • 举报
回复
这几句在HAML 中写,或者在界面重绘时写
x624722359 2012-04-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

引用 2 楼 error_code 的回复:
Microsoft.Windows.Controls.DataGrid dg=new Microsoft.Windows.Controls.DataGrid();
dg.AlternatingRowBackground="Gray" //背景斑马线里的灰色
dg.AlternationCount="2" //每2行一行灰色的


这就是正……
[/Quote]
正解个飞机。不对
  • 打赏
  • 举报
回复
7楼里的这段代码
<namespc:BackgroundConverter x:Key="myConverter"/>


能不能具体帮我解释一下这段代码的含义

而且放在那个位置?

我写了总是报错!
namhyuk 2010-03-24
  • 打赏
  • 举报
回复
用WPF Toolkit里的DataGrid吧。
  • 打赏
  • 举报
回复
11楼 :写在哪里

  • 打赏
  • 举报
回复
[Quote=引用 2 楼 error_code 的回复:]
Microsoft.Windows.Controls.DataGrid dg=new Microsoft.Windows.Controls.DataGrid();
dg.AlternatingRowBackground="Gray" //背景斑马线里的灰色
dg.AlternationCount="2" //每2行一行灰色的
[/Quote]

这就是正解
  • 打赏
  • 举报
回复
楼主,问题解决没?我遇到跟你一样的问题,希望获得你的帮助

谢谢!
51snail 2010-03-03
  • 打赏
  • 举报
回复
<namespc:ListViewItemStyleSelector x:Key="myStyleSelector"/>
是什么意思啊?这句代码放在什么地方啊?
缭绕飘渺 2010-03-03
  • 打赏
  • 举报
回复
上面的XML语句都要放到资源的样式设计语句里面
WPF和Winform不一样
你看下wpf的绑定介绍以及样式的东西
我也是刚开始接触
缭绕飘渺 2010-03-02
  • 打赏
  • 举报
回复
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"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath=FavoriteCity}"
Header="Favorite City" Width="120"/>
</GridView>
</ListView.View>
</ListView>




方法 2:从 ListView 中派生一个新类以使背景色产生交替效果

下面的示例演示如何定义从 ListView 中派生的类。此类将重写 PrepareContainerForItemOverride 方法,以便创建具有交替 Background 颜色的行。

C# 复制代码
public class SubListView : ListView
{
protected override void
PrepareContainerForItemOverride(DependencyObject element,
object item)
{
base.PrepareContainerForItemOverride(element, item);
if (View is GridView)
{
int index = ItemContainerGenerator.IndexFromContainer(element);
ListViewItem lvi = element as ListViewItem;
if (index % 2 == 0)
{
lvi.Background = Brushes.LightBlue;
}
else
{
lvi.Background = Brushes.Beige;
}
}
}
}




下面的示例演示如何创建此类的实例。namespc 前缀映射到 公共语言运行库 (CLR) 命名空间和其中定义了 StyleSelector 的对应程序集。

XAML 复制代码
<namespc:SubListView
ItemsSource="{Binding Source={StaticResource EmployeeData},
XPath=Employee}">
<namespc:SubListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding XPath=FirstName}"
Header="First Name" Width="120"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath=LastName}"
Header="Last Name" Width="120"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath=FavoriteCity}"
Header="Favorite City" Width="120"/>
</GridView>
</namespc:SubListView.View>
</namespc:SubListView>




方法 3:使用 StyleSelector 使背景色产生交替效果

下面的示例演示如何定义一个为行定义 Style 的 StyleSelector。此示例依据行索引定义 Background 颜色。

C# 复制代码
public class ListViewItemStyleSelector : StyleSelector
{
public override Style SelectStyle(object item,
DependencyObject container)
{
Style st = new Style();
st.TargetType = typeof(ListViewItem);
Setter backGroundSetter = new Setter();
backGroundSetter.Property = ListViewItem.BackgroundProperty;
ListView listView =
ItemsControl.ItemsControlFromItemContainer(container)
as ListView;
int index =
listView.ItemContainerGenerator.IndexFromContainer(container);
if (index % 2 == 0)
{
backGroundSetter.Value = Brushes.LightBlue;
}
else
{
backGroundSetter.Value = Brushes.Beige;
}
st.Setters.Add(backGroundSetter);
return st;
}
}




下面的示例演示如何为 StyleSelector 定义 ResourceKey。namespc 前缀映射到 CLR 命名空间和其中定义了 StyleSelector 的对应程序集。有关更多信息,请参见 XAML 命名空间和命名空间映射。

XAML 复制代码
<namespc:ListViewItemStyleSelector x:Key="myStyleSelector"/>




下面的示例演示如何将 ListView 的 ItemContainerStyleSelector 属性设置为此 StyleSelector 资源。

XAML 复制代码
<ListView
ItemsSource="{Binding Source={StaticResource EmployeeData},
XPath=Employee}"
ItemContainerStyleSelector="{DynamicResource myStyleSelector}" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding XPath=FirstName}"
Header="First Name" Width="120"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath=LastName}"
Header="Last Name" Width="120"/>
<GridViewColumn DisplayMemberBinding="{Binding XPath=FavoriteCity}"
Header="Favorite City" Width="120"/>
</GridView>
</ListView.View>
</ListView>




在 ListViewItem 集合中进行更改后更新 ListView

如果从 ListView 控件中添加或移除 ListViewItem,您必须更新 ListViewItem 控件以便重新创建交替的 Background 颜色。下面的示例演示如何更新 ListViewItem 控件。

C# 复制代码
ICollectionView dataView =
CollectionViewSource.GetDefaultView(theListView.ItemsSource);
dataView.Refresh();



51snail 2010-03-01
  • 打赏
  • 举报
回复
引用 5 楼 xiamiking 的回复:
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

有没有更好点的方法啊 比如获取到奇数行然后通过样式修改行的颜色
xiamiking 2010-03-01
  • 打赏
  • 举报
回复
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
51snail 2010-03-01
  • 打赏
  • 举报
回复
引用 2 楼 error_code 的回复:
Microsoft.Windows.Controls.DataGrid dg=new Microsoft.Windows.Controls.DataGrid();
dg.AlternatingRowBackground="Gray"  //背景斑马线里的灰色
dg.AlternationCount="2"  //每2行一行灰色的


用wpf的ListView控件怎么实现啊 我就是获取不到ListView中的行。本人刚刚接触WPF 请多多帮忙谢谢了
vssvss 2010-03-01
  • 打赏
  • 举报
回复
引用 2 楼 error_code 的回复:
Microsoft.Windows.Controls.DataGrid dg=new Microsoft.Windows.Controls.DataGrid();
dg.AlternatingRowBackground="Gray"  //背景斑马线里的灰色
dg.AlternationCount="2"  //每2行一行灰色的

顶这个
Error_Code 2010-03-01
  • 打赏
  • 举报
回复
Microsoft.Windows.Controls.DataGrid dg=new Microsoft.Windows.Controls.DataGrid();
dg.AlternatingRowBackground="Gray" //背景斑马线里的灰色
dg.AlternationCount="2" //每2行一行灰色的
波导终结者 2010-03-01
  • 打赏
  • 举报
回复
我还以为真的要画斑马线……

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧