在Sl中只要绑定到实现IEnumerable接口的集合,都会有一个默认的视图被隐式的插入到源和目标之间,这个视图存储着当前项的信息,也支持
排序,分组,过滤,导航这些功能,这里主要总结了PagedCollectionView这个视图的几点使用。
public class Photo
{
public string Name { get; set; }
public DateTime date { get; set; }
public int Id { get; set; }
}
public class Photos
{
public List<Photo> GetList()
{
var list = new List<Photo>
{
new Photo{Name="Simens",Id=1,date=Convert.ToDateTime("2010/8/10")},
new Photo{Name="Sunshine",Id=2,date=Convert.ToDateTime("2010/8/13")},
new Photo{Name="TreeInforest",Id=3,date=Convert.ToDateTime("2010/8/15")},
new Photo{Name="WaterNaturl",Id=4,date=Convert.ToDateTime("2010/8/12")},
};
return list;
}
<data:DataGrid AutoGenerateColumns="True" Height="131" HorizontalAlignment="Left" Margin="43,75,0,0" Name="dataGrid1"
CanUserSortColumns="False" VerticalAlignment="Top" Width="289" />
<Button Content="SortByName" Height="23" HorizontalAlignment="Left" Margin="52,221,0,0" Name="SortByName" VerticalAlignment="Top"
Width="75" Click="SortByName_Click" />
<Button Content="SortByDateTime" Height="23" HorizontalAlignment="Left" Margin="141,221,0,0" Name="SortByDateTime"
VerticalAlignment="Top" Width="75" Click="SortByDateTime_Click" />
PagedCollectionView p = new PagedCollectionView(new Photos().GetList());
this.dataGrid1.ItemsSource = p;
这时,我们已经可以在DataGrid中看到关联的数据了,接下来,继续完成排序的工作,用属性名与ListSortDirection枚举值创建SortDescription对象
p.SortDescriptions.Add(new SortDescription(“Name”, ListSortDirection.Descending));
对于SortDescriptions而言,其可以同时对多个属性进行排序,按照添加至SortDescriptions的顺序表示优先级
p.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Descending));
p.SortDescriptions.Add(new date", ListSortDirection.Descending));
PagedCollectionView p = new PagedCollectionView(new Photos().GetList());
public MainPage()
{
InitializeComponent();
this.dataGrid1.ItemsSource = p;
}
private void SortByName_Click(object sender, RoutedEventArgs e)
{
Help("Name");
}
private void SortByDateTime_Click(object sender, RoutedEventArgs e)
{
Help("date");
}
private void Help(string property)
{
if (p.SortDescriptions.Count > 0 && p.SortDescriptions[0].PropertyName == property &&
p.SortDescriptions[0].Direction == ListSortDirection.Ascending)
{
p.SortDescriptions.Clear();
p.SortDescriptions.Add(new SortDescription(property, ListSortDirection.Descending));
}
else
{
p.SortDescriptions.Clear();
p.SortDescriptions.Add(new SortDescription(property, ListSortDirection.Ascending));
}
}
p.GroupDescriptions.Add(new PropertyGroupDescription("date"));
public class DateValueConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((DateTime)value).ToShortDateString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
p.GroupDescriptions.Add(new PropertyGroupDescription("date",new DateValueConverter()));
这样我们可以再看一看页面效果:
<data:DataGrid.RowGroupHeaderStyles>
<Style TargetType="data:DataGridRowGroupHeader">
<Setter Property="PropertyNameVisibility" Value="Collapsed" />
<Setter Property="Background" Value="#FF112255" />
<Setter Property="Foreground" Value="#FFEEEEEE" />
<Setter Property="SublevelIndent" Value="15" />
<Setter Property="ItemCountVisibility" Value="Collapsed" />
</Style>
</data:DataGrid.RowGroupHeaderStyles>
private void Previous_Click(object sender, RoutedEventArgs e)
{
p.MoveCurrentToPrevious();
if (p.IsCurrentBeforeFirst)
{
p.MoveCurrentToLast();
}
MessageBox.Show(p.CurrentPosition.ToString());
}
private void Next_Click(object sender, RoutedEventArgs e)
{
p.MoveCurrentToNext();
if (p.IsCurrentAfterLast)
p.MoveCurrentToFirst();
MessageBox.Show((p.CurrentItem as Photo).Id.ToString());
}
public Predicate<Object> Filter { get; set; }
p.Filter = (obj) =>{ return (obj as Photo).Id > 3; };
<StackPanel Height="31" HorizontalAlignment="Left" Margin="98,176,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="92" Orientation="Horizontal">
<CheckBox Content="1" Height="16" Name="checkBox1" Click="checkBox_Click" />
<CheckBox Content="2" Height="16" Name="checkBox2" Click="checkBox_Click" />
<CheckBox Content="3" Height="16" Name="checkBox3" Click="checkBox_Click"/>
</StackPanel>
using System;
using System.Linq;
using System.Linq.Expressions;
namespace NewWork
{
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Predicate<T> ConvertToPredicate<T>(this Func<T, bool> func)
{
return new Predicate<T>(func);
} }}
private void checkBox_Click(object sender, RoutedEventArgs e)
{
var els = this.stackPanel1.Children;
var predicate = PredicateBuilder.False<object>();
foreach (var el in els)
{
var cb = el as CheckBox;
if (cb.IsChecked == true)
{
predicate = predicate.Or(s => ((Photo)s).Id > Convert.ToInt32(cb.Content));
}
}
p.Filter = predicate.Compile().ConvertToPredicate<object>();
}