<!--Xaml-->
<Window x:Class="wpf_ListBox.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListBox排序-分组" Height="461" Width="431" SizeToContent="Manual" ResizeMode="CanResizeWithGrip" WindowStartupLocation="CenterScreen">
<Grid>
<ListBox Margin="12,16,12,134" Background="AliceBlue" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Name="lbData" ItemsSource="{Binding}">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>
</Border>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Label Content="{Binding Id}"></Label>
<Label Content="{Binding Name}"></Label>
<Label Content="{Binding Sex}"></Label>
<Label Content="{Binding Age}"></Label>
<Label Content="{Binding Address}"></Label>
<Label Content="{Binding DateTime}"></Label>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Height="23" HorizontalAlignment="Left" Margin="54,0,0,105" Name="btnIdOrder" VerticalAlignment="Bottom" Width="75" Click="btnIdOrder_Click">编号</Button>
<Button Height="23" Margin="135,0,199,105" Name="btnNameOrder" VerticalAlignment="Bottom" Click="btnNameOrder_Click">姓名</Button>
<Button Height="23" Margin="0,0,118,105" Name="btnSexOrder" VerticalAlignment="Bottom" Click="btnSexOrder_Click" HorizontalAlignment="Right" Width="75">性别</Button>
<Button Height="23" HorizontalAlignment="Right" Margin="0,0,37,105" Name="btnAgeOrder" VerticalAlignment="Bottom" Width="75" Click="btnAgeOrder_Click">年龄</Button>
<Button Height="23" Margin="54,0,199,76" Name="btnDateGroup" VerticalAlignment="Bottom" Click="btnDateGroup_Click">按时间进行分组</Button>
</Grid>
</Window>
//后置代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.ComponentModel;
using System.Globalization;
namespace wpf_ListBox
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = GetDataTable();
}
#region 数据
public DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Sex", typeof(string));
dt.Columns.Add("Age", typeof(int));
dt.Columns.Add("Address", typeof(string));
dt.Columns.Add("DateTime", typeof(DateTime));
dt.Rows.Add(1, "张文广", "男", 17, "黑龙江省哈尔滨市", Convert.ToDateTime("2009-1-5"));
dt.Rows.Add(2, "李文才", "男", 22, "黑龙江省哈尔滨市", Convert.ToDateTime("2009-1-5"));
dt.Rows.Add(3, "王小虎", "女", 12, "黑龙江省哈尔滨市", Convert.ToDateTime("2009-1-4"));
dt.Rows.Add(4, "刘蒙蒙", "男", 25, "黑龙江省哈尔滨市", Convert.ToDateTime("2009-1-5"));
dt.Rows.Add(5, "赵向楠", "男", 36, "黑龙江省哈尔滨市", Convert.ToDateTime("2009-1-5"));
dt.Rows.Add(6, "杨言功", "男", 14, "黑龙江省哈尔滨市", Convert.ToDateTime("2009-1-4"));
dt.Rows.Add(7, "刘宇成", "男", 19, "黑龙江省哈尔滨市", Convert.ToDateTime("2009-1-5"));
dt.Rows.Add(8, "于江振", "男", 23, "黑龙江省哈尔滨市", Convert.ToDateTime("2009-1-5"));
return dt;
}
#endregion
private void btnIdOrder_Click(object sender, RoutedEventArgs e)
{
SortHelper("Id");
}
private void btnNameOrder_Click(object sender, RoutedEventArgs e)
{
SortHelper("Name");
}
private void btnSexOrder_Click(object sender, RoutedEventArgs e)
{
SortHelper("Sex");
}
private void btnAgeOrder_Click(object sender, RoutedEventArgs e)
{
SortHelper("Age");
}
private void btnDateGroup_Click(object sender, RoutedEventArgs e)
{
GroupHelper();
}
#region 排序-分组
void SortHelper(string propertyName)
{
ICollectionView view = CollectionViewSource.GetDefaultView(this.lbData.Items); //加载源数据
//检查该视图是否已经根据当前属性按升序排序
if (view.SortDescriptions.Count > 0
&& view.SortDescriptions[0].PropertyName == propertyName
&& view.SortDescriptions[0].Direction == ListSortDirection.Descending)
{
//已经倒序排序,切换为升序排序
view.SortDescriptions.Clear();
view.SortDescriptions.Add(new SortDescription(propertyName, ListSortDirection.Ascending));
}
else
{
//倒序排序
view.SortDescriptions.Clear();
view.SortDescriptions.Add(new SortDescription(propertyName, ListSortDirection.Descending));
}
}
void GroupHelper()
{
ICollectionView view = CollectionViewSource.GetDefaultView(this.lbData.Items);
view.GroupDescriptions.Clear();
view.GroupDescriptions.Add(new PropertyGroupDescription("DateTime", new DateTimeToDateConverter()));
}
#endregion
}
/// <summary>
/// 你会发现根据时间分组或许不是一个好主意。因为DateTime包括日期和时间两部分,第条数据几乎都有一个唯一的值,如:2009-1-5 05:06:07,
/// 这使得很多分组都只有一项!为了修正这个问题,可以使用PropertyGroupDescription类的重载构造函数,这样就可以在把它用于分组之前对属
/// 性值进行调整。构造函数允许我们传入一个值转换器!
/// view.GroupDescriptions.Add(new PropertyGroupDescription("DateTime", new DateTimeToDateConverter()));
/// 所以写了一个DateTimeToDateConverter类把DateTime转换为一个更适合进行分组的字符串
/// </summary>
class DateTimeToDateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((DateTime)value).ToString("MM/dd/yyyy"); //以这种格式进行分组
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new NotSupportedException();
}
}
}