• SILVERLIGHT的LISTBOX数据排序


    查了下资料,LISTBOX不带排序功能,所以要从数据源入手,自己实现一个List的Sort方法,主要就是那个icomparer接口的实现。

    原文在这里

    下面是测试代码,只是简单的对INT进行排序,把那篇文章的内容简化了。熟悉下最基本的实现。

     1 <UserControl x:Class="SL.ListboxSort.MainPage"
     2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     6     mc:Ignorable="d"
     7     d:DesignHeight="300" d:DesignWidth="400">
     8 
     9     <Grid x:Name="LayoutRoot" Background="White">
    10         <ListBox Loaded="LB1_Loaded" x:Name="LB1" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top" Width="130" Margin="120,0,0,0">
    11             <ListBox.ItemTemplate>
    12                 <DataTemplate>
    13                     <TextBlock Text="{Binding Name}" />
    14                 </DataTemplate>
    15             </ListBox.ItemTemplate>
    16         </ListBox>
    17     </Grid>
    18 </UserControl>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections;
    using System.Reflection;
    
    namespace SL.ListboxSort
    {
        public partial class MainPage : UserControl
        {
             
            
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void LB1_Loaded(object sender, RoutedEventArgs e)
            {
    
                //初始化测试数据集
                List<LBData> datalist = new List<LBData>();
                datalist.Add(new LBData(Name="zhang1",20));
                datalist.Add(new LBData(Name = "zhang2", 22));
                datalist.Add(new LBData(Name = "zhang3", 23));
                datalist.Add(new LBData(Name = "zhang4", 24));
                datalist.Add(new LBData(Name = "zhang5", 25));
                datalist.Add(new LBData(Name = "zhang6", 25));
                datalist.Add(new LBData(Name = "zhang7", 1));
                datalist.Add(new LBData(Name = "zhang8", 2));
    
                //确定自己定义的排序规则
                GComparer<LBData> gc = new GComparer<LBData>();
                datalist.Sort(gc);
    
                //数据控件绑定
                LB1.ItemsSource = datalist;
            }
        }
    
        #region 数据集
        public class LBData:INotifyPropertyChanged
        {
            public LBData(string name, int rate)
            {
                Name = name;
                Rate = rate;
            }
            
            public string Name
            {
                get;
                set;
            }
            public int Rate
            { get; set; }
    
            #region impliment
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = this.PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }
        #endregion 
    
        #region 自定义的一个LIST,因为测试只是最简单的实现,发现没必要用上这个,在资料文章里面这里还提供了升序还是降序排列的功能
        public class Mylist<T> : List<T>
        {
            public void Sort()
            {
                GComparer<T> gc = new GComparer<T>();
                base.Sort(gc);
                
            }
        }
        #endregion
    
        #region IComparer接口实现,用来自定义排序规则
        public class GComparer<T> : IComparer<T>
        {
            public int Compare(T x, T y)
            {
                PropertyInfo property = typeof(T).GetProperty("Rate");//要包括程序集System.Reflection。Rate是用于在数据集LBDATA里面找到这个属性
                int id1, id2;
                id1 = Convert.ToInt32(property.GetValue(x,null).ToString());
                id2 = Convert.ToInt32(property.GetValue(y, null).ToString());
                //最关键的其实就是这句,前面都是数据的转换
                return id1.CompareTo(id2);
            }
        }
    #endregion
  • 相关阅读:
    yaml 文件解析
    python 实现自动部署测试环境
    运行ride.py报错,闪退
    selenium 配置ie11 浏览器
    自动化测试(1)selenium+python+chrome 连接测试
    scrapy爬虫框架
    drf内置排序源码
    celery基本使用
    C# 如何复制(拷贝)Label控件上的文本【新方法】
    C# 使用PictureBox实现图片按钮控件
  • 原文地址:https://www.cnblogs.com/matoo/p/2466769.html
Copyright © 2020-2023  润新知