Item背景颜色交替变化的ListBox

PS:由于粗心大意,没有附上原文章的地址,对此我深表歉意,特此声明,

原文地址:http://home.cnblogs.com/u/lhxarcher/

原文作者:暗影吉他手

先展示一下运行效果

这种效果的控件做起来并不难,而且MSDN上已经有了一篇文章(http://msdn.microsoft.com/zh-cn/library/ms750769.aspx),谈到了如何设计一个每行背景色可变的ListView。但是众所周知WPF和Silverlight,尤其是Silverlight For WP7总是有点差距的,你会发现对于方法一,Silverlight不支持在Style中的Setter里面设置Binding,对于方法三,Silverlight不知道ItemContainerStyleSelector是神马玩意,看起来只有方法二能用,但是想实现“在Items集合改变后更新ListBox”的效果,文章最后的方法也不能用,因为CollectionViewSource.GetDefaultView方法在Silverlight里面也浮云了。

于是我们采用方法二,派生一个ListBox,然后想办法动态更新它。

主要的操作步骤如下所示:

1)定义一个命名空间,然后在命名空间下定义一个继承与listbox控件的类,并重载一些函数,参考代码如下所示:

namespace listBoxControl //记住个命名空间,我们是要使用这个命名空间
{
    public class myColorListBox : ListBox//继承ListBox控件,并重载基类的属性
    {
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
            int index = ItemContainerGenerator.IndexFromContainer(element);  // element实际上就是正要显示的ListBoxItem 
            ListBoxItem lvi = element as ListBoxItem;
            if (index % 2 == 0)
            {
                lvi.Background = new SolidColorBrush(Colors.Green);//设置偶数情况下,iems项的背景颜色
            }
            else
            {
                lvi.Background = new SolidColorBrush(Colors.Red);//设置奇数情况下,listboxItemx项的背景颜色
            }
        }
        //当前选项改变后,触发的事件
        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                for (int i = e.OldStartingIndex; i < Items.Count; i++)
                {
                    ListBoxItem lvi = ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
                    if (i % 2 == 0)
                    {
                        lvi.Background = new SolidColorBrush(Colors.Green);
                    }
                    else
                    {
                        lvi.Background = new SolidColorBrush(Colors.Red);
                    }
                }
            }
        }
    }
}

2)将该命名空间添加到Page页面的前台代码,也就是将该命名空间注册到该页面的前台代码中

  xmlns:myLb ="clr-namespace:listBoxControl"//将改行代码添加到page页面的前台代码中

3)向Page内容容器中注册从listBox派生的控件类,参考代码如下所示

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

     //这一步的myLb和第二部的中的myLb是一致的,在这里我们可以像操作基类Listbox一样操作此处的派生类
            <myLb:myColorListBox  x:Name="mybox"  Margin="0,6,0,-6"></myLb:myColorListBox>
        </Grid>

4)进入页面后台,编写逻辑处理代码

  public partial class colorChangedPage : PhoneApplicationPage
    {
        public colorChangedPage()
        {
            InitializeComponent();

    //
            List<string> tt = new System.Collections.Generic.List<string>();
            tt.Add("xingchen");
            tt.Add("xiaohua");
            tt.Add("xiaoming");
            mybox.ItemsSource = tt;//指定数据源,PS,这里仅仅是指定一个简单的数据源,当然我们可以根据需要进行扩展
            mybox.FontSize = 40; //设置item中字体中的大小
        }
    }

5)所有的代码准备工作已经书写完毕,单击F5或者单击IDE中的Debug按钮,就会看到看到上图中的运行效果!

原文地址:https://www.cnblogs.com/xingchen/p/1978614.html