WPF动态类动态表头实现排序
采用了动态类的表格我们要显示排序的话也需要使用反射来实现,关键代码如下:
private void TextBlockMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TextBlock textBlock = sender as TextBlock;
string proname = textBlock.Tag.ToString();
if (isclicked == false)
{
HeadmasterDayReportModels = HeadmasterDayReportModels.OrderBy(x =>
{
PropertyInfo[] proInfos = x.GetType().GetProperties();
PropertyInfo pi = proInfos.Where(info => info.Name == "DicProperty").FirstOrDefault();
Dictionary<string, object> pairs = pi.GetValue(x, null) as Dictionary<string, object>;
return Convert.ToDouble(pairs.Where(pa => pa.Key == proname).ToList()[0].Value.ToString().Replace("%", ""));
}).ToList();
this.headmastergrid.ItemsSource = HeadmasterDayReportModels;
isclicked = true;
}
else
{
HeadmasterDayReportModels = HeadmasterDayReportModels.OrderByDescending(x =>
{
PropertyInfo[] proInfos = x.GetType().GetProperties();
PropertyInfo pi = proInfos.Where(info => info.Name == "DicProperty").FirstOrDefault();
Dictionary<string, object> pairs = pi.GetValue(x, null) as Dictionary<string, object>;
return Convert.ToDouble(pairs.Where(pa => pa.Key == proname).ToList()[0].Value.ToString().Replace("%",""));
}).ToList();
this.headmastergrid.ItemsSource = HeadmasterDayReportModels;
isclicked = false;
}
}