日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF中设置ListView的Items颜色交替显示

發布時間:2023/12/13 asp.net 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF中设置ListView的Items颜色交替显示 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2008/02/28 17:32

i當ListView綁定數據源后,這個效果讓我無從下手,

這個問題一直困擾著我,后來我在CSDN上發貼求助,問題終于得以解決,這是一位大大給的回復:

以下各節提供了三種方法,用于創建各行的 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();

轉載于:https://www.cnblogs.com/seven_cheng/archive/2010/05/14/1735568.html

總結

以上是生活随笔為你收集整理的WPF中设置ListView的Items颜色交替显示的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。