Silverlight学习笔记二(续)
2008-12-26 11:54:05 来源:WEB开发网完整的代码如下:
1 publicpartialclassProductsList:UserControl
2 {
3 privateList<Products>products_list;
4 privatePagerpager=null;
5 //当前排序的字段
6 privatestringsortFiled="ProductID";
7 //当前排序的方向
8 privatestringdir="asc";
9 //当前排序的ColumnHeader
10 privateDataGridColumnHeadersortHeader;
11
12 publicProductsList()
13 {
14 InitializeComponent();
15 MouseHelper.SetMouseWheelHandler(OnMouseWheel);
16 BindGrid();
17 }
18
19 privatevoidBindGrid()
20 {
21 canvas.Visibility=Visibility.Visible;
22 varclient=newNorthWindWCF.NorthWindWCFClient();
23 client.GetProductsPagingCompleted+=newEventHandler<GetProductsPagingCompletedEventArgs>(client_GetProductsPagingCompleted);
24 intstart=1;
25 intlimit=10;
26 if(pager!=null)
27 start=(pager.PageIndex-1)*10+1;
28 client.GetProductsPagingAsync(start,limit,sortFiled,dir);
29 }
30
31 publicvoidOnMouseWheel(objectsender,MouseWheelHandlerEventArgsargs)
32 {
33 if(Parent==null)return;
34 sv.ScrollToVerticalOffset(sv.VerticalOffset-args.Delta*15);
35 }
36
37 privatevoidclient_GetProductsPagingCompleted(objectsender,GetProductsPagingCompletedEventArgse)
38 {
39 if(e.Error==null)
40 {
41 products_list=e.Result.ToList();
42 dgData.ItemsSource=products_list;
43 if(pager==null)
44 {
45 pager=newPager(e.TotalPage,2);
46 pager.Click+=newPager.PagerButtonClick(pager_Click);
47 pager.PageIndex=1;
48 spPager.Children.Clear();
49 spPager.Children.Add(pager);
50 }
51 }
52 else
53 MessageBox.Show(e.Error.Message);
54 canvas.Visibility=Visibility.Collapsed;
55 }
56
57 privatevoidpager_Click(objectsender,RoutedEventArgse)
58 {
59 BindGrid();
60 }
61
62 privatevoiddgData_MouseLeftButtonDown(objectsender,MouseButtonEventArgse)
63 {
64 varu=fromelementinVisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null),dgData)
65 whereelementisDataGridColumnHeader
66 selectelement;
67
68 if(u.Count()==1)
69 {
70 //鼠标点击的ColumnHeader
71 DataGridColumnHeaderheader=(DataGridColumnHeader)u.Single();
72 //要排序的字段
73 string_newsort=header.Content.ToString();
74 //清除名称中的↑和↓
75 _newsort=removeSortStateFlag(_newsort);
76 //判断排序方向
77 if(_newsort==sortFiled)
78 dir=dir=="desc"?"asc":"desc";
79 else
80 {
81 //如果新的排序字段和当前的排序字段不一样,则要清楚当前排序字段的排序标志(↑↓)
82 if(sortHeader!=null)
83 sortHeader.Content=removeSortStateFlag(sortHeader.Content.ToString());
84 dir="asc";
85 }
86 sortFiled=_newsort;
87 //加上排序标志
88 header.Content=dir=="asc"?_newsort+"↑":_newsort+"↓";
89 //设置新的排序字段为当前排序字段
90 sortHeader=header;
91 BindGrid();
92 e.Handled=true;
93 }
94 else
95 e.Handled=false;
96 }
97
98 privatestringremoveSortStateFlag(stringdir)
99 {
100 intpos=dir.IndexOfAny(newchar[]{'↑','↓'});
101 if(pos>0)dir=dir.Substring(0,pos-1);
102 returndir;
103 }
104
最终效果:
Tags:Silverlight 学习 笔记
编辑录入:爽爽 [复制链接] [打 印]- ››笔记本蓝屏错误代码0X0000000A故障处理
- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››学习网站日志分析:网站日志分析各项指标
- ››学习:Ylmf OS安装全过程(视频)
- ››学习了SEO可以做哪些事
- ››学习 Linux,101: 保持文件系统的完整性
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››学习Java,C等程序开发的十大秘诀
更多精彩
赞助商链接