加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

wpf ListView 隔行 (各行) 显示 不同 背景 颜色

(2017-07-20 17:06:21)
分类: .NET

此示例演示您可用于使 ListView 中各行的 Background 颜色产生交替效果的三种方法。

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

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

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

 

 

下面的示例为 IValueConverter 定义 ResourceKey

 
"myConverter"/>

 

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

 
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

 
"theListView" 

          ItemsSource="{Binding Source={StaticResource EmployeeData}, 

                                        XPath=Employee}"

          ItemContainerStyle="{StaticResource myItemStyle}" >

  

    

      "{Binding XPath=FirstName}" 

                      Header="First Name" Width="120"/>

      "{Binding XPath=LastName}" 

                      Header="Last Name" Width="120"/>

      "{Binding XPath=FavoriteCity}" 

                      Header="Favorite City" Width="120"/>

    

  

 

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

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

 
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 的对应程序集。

 
<<span style="color: rgb(0, 0, 0);">namespc:SubListView

      ItemsSource="{Binding Source={StaticResource EmployeeData}, 

                                        XPath=Employee}">

  

    

      "{Binding XPath=FirstName}" 

                      Header="First Name" Width="120"/>

      "{Binding XPath=LastName}" 

                      Header="Last Name" Width="120"/>

      "{Binding XPath=FavoriteCity}" 

                      Header="Favorite City" Width="120"/>

    

  

 

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

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

 
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 定义 ResourceKeynamespc 前缀映射到 CLR 命名空间和其中定义了 StyleSelector 的对应程序集。有关更多信息,请参见 XAML 命名空间和命名空间映射

 
"myStyleSelector"/>

 

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

 
<<span style="color: rgb(0, 0, 0);">ListView 

      ItemsSource="{Binding Source={StaticResource EmployeeData}, 

                                        XPath=Employee}"

      ItemContainerStyleSelector="{DynamicResource myStyleSelector}" >      

  

    

      "{Binding XPath=FirstName}" 

                      Header="First Name" Width="120"/>

      "{Binding XPath=LastName}" 

                      Header="Last Name" Width="120"/>

      "{Binding XPath=FavoriteCity}" 

                      Header="Favorite City" Width="120"/>

    

  

 

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

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

 
ICollectionView dataView =

  CollectionViewSource.GetDefaultView(theListView.ItemsSource);

dataView.Refresh();

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有