WEB开发网
开发学院图形图像Flash Silverlight学习笔记二(续) 阅读

Silverlight学习笔记二(续)

 2008-12-26 11:54:05 来源:WEB开发网   
核心提示: 完整的代码如下:1publicpartialclassProductsList:UserControl2{3privateList<Products>products_list;4privatePagerpager=null;5//当前排序的字段6privatestringso

完整的代码如下:

 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

最终效果:

Silverlight学习笔记二(续)

上一页  1 2 

Tags:Silverlight 学习 笔记

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接