silverlight datagrid控件,单击标题自定义排序

直接贴代码

 

<sdk:DataGrid AutoGenerateColumns="False" CanUserSortColumns="False" CanUserReorderColumns="False"  IsReadOnly="True" HorizontalAlignment="Left" Margin="0,0,0,86" x:Name="ErrorCodeGrid" Width="662" Grid.Row="1" MouseLeftButtonDown="ErrorCodeGrid_MouseLeftButtonDown">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn Header="日期" Binding="{Binding Date}" Width="110"/>
        <sdk:DataGridTextColumn Header="地市" Binding="{Binding Area}" Width="110"/>
        <sdk:DataGridTextColumn Header="发送/接收" Binding="{Binding Pattern}" Width="95" />
        <sdk:DataGridTextColumn Header="短信类型" Binding="{Binding EmailType}" Width="115"/>
        <sdk:DataGridTextColumn Header="机型" Binding="{Binding PhoneType}" Width="115"/>
        <sdk:DataGridTextColumn Header="比例" Binding="{Binding Ratio}" Width="*"/>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

 

//下面为单机事件

private void ErrorCodeGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var u = from element in VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(null), ErrorCodeGrid)
            where element is DataGridColumnHeader
            select element;
    if (u.Count() == 1)
    {
        //鼠标点击的ColumnHeader
        DataGridColumnHeader header = (DataGridColumnHeader)u.Single();
        //要排序的字段
        string _newSort = header.Content.ToString();
        if (dir == "desc")
        {
            dir = "asc";
        }
        else
        {
            dir = "desc";
        }
        sortFiled = _newSort;
        sortHeader = header;
        SortData(dir, sortFiled);
        e.Handled = true;
    }
    else
    {
        e.Handled = false;
    }
}

 

//排序事件

void SortData(string sortType, string sortFiled)
{
    dDetailList = ChangeResult(DetailList);
    if (sortType == "desc")
    {
        switch (sortFiled)
        {
            case "比例":
                dDetailList = dDetailList.OrderByDescending(u => u.Ratio).ToList();
                break;
 
            default:
                break;
        }
    }
    else
    {
        switch (sortFiled)
        {
            case "比例":
                dDetailList = dDetailList.OrderBy(u => u.Ratio).ToList();
                break;
            default:
                break;
        }
    }
    DetailList = ChangeBackResult(dDetailList);
    fenye("first");
}

 

 

//分页事件

void fenye(string done)
       {
           TotalCount = DetailList.Count();
           if (TotalCount % PageSize == 0)
           {
               TotalPage = TotalCount / PageSize;
           }
           else
           {
               TotalPage = TotalCount / PageSize + 1;
           }

           first.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
           pre.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
           next.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
           last.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));

           if (done == "first")
           {
               CurrentPage = 1;
           }
           else if (done == "last")
           {
               CurrentPage = TotalPage;
               next.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
               last.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
           }
           else if (done == "pre")
           {
               if (CurrentPage > 1)
                   CurrentPage -= 1;
               else
               {
                   first.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                   pre.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
               }
           }
           else if (done == "next")
           {
               if (CurrentPage < TotalPage)
                   CurrentPage += 1;
               else
               {
                   next.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
                   last.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
               }
           }

           if (TotalPage == 1)
           {
               first.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
               pre.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
               next.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
               last.Foreground = new SolidColorBrush(Color.FromArgb(255, 178, 187, 190));
           }

           pageShow.Text = CurrentPage + "/" + TotalPage;
           List<ErrorCodeDetailBind> query = DetailList.Take(CurrentPage * PageSize).Skip((CurrentPage - 1) * PageSize).ToList();
           ErrorCodeGrid.ItemsSource = query;
       }

转载于:https://www.cnblogs.com/honkcal/archive/2012/02/15/2353086.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页