半岛权威十大直营(官方)网站

Item背景颜色交替变化的ListBox

转帖|其它|编辑:郝浩|2011-03-10 13:22:41.000|阅读 1717 次

概述:众所周知WPF和Silverlight,尤其是Silverlight For WP7总是有点差距的,你会发现对于方法一,Silverlight不支持在Style中的Setter里面设置Binding,对于方法三,Silverlight不知道ItemContainerStyleSelector是神马玩意,看起来只有方法二能用,但是想实现“在Items集合改变后更新ListBox”的效果,文章最后的方法也不能用,因为CollectionViewSource.GetDefaultView方法在 Silverlight里面也浮云了。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

先展示一下运行效果:

  这种效果的控件做起来并不难,而且MSDN上已经有了一篇文章,谈到了如何设计一个每行背景色可变的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按钮,就会看到看到上图中的运行效果!

 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@wqylolg.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP