[英]Pass datagrid.ItemsSource as a generic list in a generic function
I am tryting to implement paggination on datagrid, which can contain different types of lists eg Teacher, Student
etc How can I call the button click event so that my Paginate function can filter that list and set the itemssource of my datagrid 我正在尝试在datagrid上实现分页,它可以包含不同类型的列表,例如
Teacher, Student
等如何调用按钮单击事件,以便我的Paginate函数可以过滤该列表并设置datagrid的itemssource
public List<T> Paginate<T>(List<T> list, int itemsPerPage, int currentPage)
{
// some code
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
this.Paginate(myDataGrid.ItemsSource,3,1);
}
If you have no restrictions to the elements and access it only by index and don't use extra filtering by object properties then you can use non-generic types in your code: 如果您对元素没有限制并且仅按索引访问它,并且不使用按对象属性进行的额外过滤,则可以在代码中使用非泛型类型:
public IList Paginate(IList list, int itemsPerPage, int currentPage)
{
// some code
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
this.Paginate(myDataGrid.ItemsSource as IList, 3, 1);
}
But you should do it only if you are ensure that ItemsSource
contains an object that implements IList
, for example, List<T>
. 但是,只有在确保
ItemsSource
包含实现IList
的对象(例如List<T>
,才应该这样做。 If you are not sure you will need to use IEnumerable
interface and iterate collection for each paging. 如果不确定,则需要使用
IEnumerable
接口并为每个分页迭代集合。
try this , for this solution you need to atlest provide information which datalist is binded 尝试一下,对于此解决方案,您需要提供绑定了哪个数据列表的信息
private void Button1_Click(object sender, RoutedEventArgs e)
{
if(boundlist=="Employee")
{
List<Employee> copy = new List<Employee>
((myDataGrid.ItemsSource as IList).OfType<Employee>());
this.Paginate<Employee>(copy,3,1);
}
else if(boundlist=="Student")
{
List<Student> copy = new List<Employee>
((myDataGrid.ItemsSource as IList).OfType<Student>());
this.Paginate<Student>(copy,3,1);
}
}
If Paginate is just for paging then just pass it as a list of objects. 如果分页仅用于分页,则将其作为对象列表传递。
private void Button1_Click(object sender, RoutedEventArgs e)
{
this.Paginate(myDataGrid.ItemsSource as IList<object>, 3, 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.