博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
silverlight datagrid控件,单击标题自定义排序
阅读量:5051 次
发布时间:2019-06-12

本文共 3599 字,大约阅读时间需要 11 分钟。

直接贴代码

 

<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

你可能感兴趣的文章
「Foundation」集合
查看>>
RijndaelManaged 加密
查看>>
Android 音量调节
查看>>
windows上面链接使用linux上面的docker daemon
查看>>
每天一个小程序—0005题(批量处理图片大小)
查看>>
js-创建对象的几种方式
查看>>
JDK JRE Java虚拟机的关系
查看>>
2018.11.20
查看>>
word20161215
查看>>
dijkstra (模板)
查看>>
编译Linux驱动程序 遇到的问题
查看>>
大型分布式网站架构技术总结
查看>>
[算法之美] KMP算法的直观理解
查看>>
EntityFramework 性能优化
查看>>
【ASP.NET开发】菜鸟时期的ADO.NET使用笔记
查看>>
android圆角View实现及不同版本号这间的兼容
查看>>
OA项目设计的能力③
查看>>
Cocos2d-x3.0 文件处理
查看>>
全面整理的C++面试题
查看>>
Activity和Fragment生命周期对比
查看>>