C# 使用分页CollectionView的WPF可编辑数据网格
我正在实现本文中提出的分页数据网格。一切正常,但我的网格中有一些复选框,当我尝试选中它们时,应用程序会抛出一个异常,因为C# 使用分页CollectionView的WPF可编辑数据网格,c#,wpf,datagrid,C#,Wpf,Datagrid,我正在实现本文中提出的分页数据网格。一切正常,但我的网格中有一些复选框,当我尝试选中它们时,应用程序会抛出一个异常,因为CollectionView不允许编辑。如何获取当前视图中的项目列表并将其转换为列表集合?我在公共类PagingCollectionView:CollectionView类中添加了以下方法以进行测试: private void RefreshGrid() { //this._currentDataGrid.DataContext = this;
CollectionView
不允许编辑。如何获取当前视图中的项目列表并将其转换为列表集合?我在公共类PagingCollectionView:CollectionView
类中添加了以下方法以进行测试:
private void RefreshGrid()
{
//this._currentDataGrid.DataContext = this; **throws exception** when clicking on
//this.Refresh(); **checkbox
List<Customer> list = this.Cast<Customer>().ToList(); //** still get the original list
this._currentDataGrid.DataContext = list;
this.Refresh();
//I also tried this
//this.Refresh();
//List<Customer> list = this.Cast<Customer>().ToList(); //** same result
//this._currentDataGrid.DataContext = list;
}
private void RefreshGrid()
{
//this.\u currentDataGrid.DataContext=this;**单击时引发异常**
//此.Refresh();**复选框
List List=this.Cast().ToList();//**仍将获取原始列表
这是。_currentDataGrid.DataContext=list;
这个。刷新();
//我也试过这个
//这个。刷新();
//List List=this.Cast().ToList();//**结果相同
//这是。_currentDataGrid.DataContext=list;
}
理想情况下,我希望只获取视图中的项目,此时我将每页的项目设置为5个,但我将获取所有16个项目。
PagingCollectionView
工作正常,只是我无法选中复选框。来自链接帖子的PagingCollectionView
没有实现IEditableCollectionView
,因此您无法更改网格中的值
不幸的是,WPF中似乎不存在标准的分页集合视图。但是Silverlight的PagedCollectionView
确实有效,可以将代码复制到项目中。我想您也可以安装Silverlight SDK并添加对System.Windows.Data.dll的引用。或者,如果您可以自己实现IEditableCollectionView
,我相信您会找到一些接受者;)
对于看起来像是最佳选择的项目,您将需要来自的所有代码
测试输出:
视图模型:
using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfApplication1.ViewModels
{
public class CustomersViewModel : NotifyBase // replace NotifyBase by implementing INotifyPropertyChanged
{
public PagedCollectionView CustomerCollection { get; private set; }
public int TotalPages { get { return (int)Math.Ceiling((double)CustomerCollection.ItemCount / (double)CustomerCollection.PageSize); } }
public int PageNumber { get { return CustomerCollection.PageIndex + 1; } }
public ICommand MoveNextCommand { get { return GetValue(() => MoveNextCommand); } set { SetValue(() => MoveNextCommand, value); } }
public ICommand MovePreviousCommand { get { return GetValue(() => MovePreviousCommand); } set { SetValue(() => MovePreviousCommand, value); } }
public CustomersViewModel()
{
this.CustomerCollection = new PagedCollectionView(new ObservableCollection<Customer>
{
new Customer(true, "Michael", "Delaney"),
new Customer(false, "James", "Ferguson"),
new Customer(false, "Andrew", "McDonnell"),
new Customer(true, "Sammie", "Hunnery"),
new Customer(true, "Olivia", "Tirolio"),
new Customer(false, "Fran", "Rockwell"),
new Customer(false, "Andrew", "Renard"),
});
this.CustomerCollection.PageSize = 3;
this.MoveNextCommand = new ActionCommand(MoveNext);
this.MovePreviousCommand = new ActionCommand(MovePrevious);
}
private void MoveNext()
{
this.CustomerCollection.MoveToNextPage();
OnPropertyChanged("PageNumber");
}
private void MovePrevious()
{
this.CustomerCollection.MoveToPreviousPage();
OnPropertyChanged("PageNumber");
}
}
public class Customer : NotifyBase // replace NotifyBase by implementing INotifyPropertyChanged
{
public bool IsActive { get { return GetValue(() => IsActive); } set { SetValue(() => IsActive, value); } }
public string FirstName { get { return GetValue(() => FirstName); } set { SetValue(() => FirstName, value); } }
public string LastName { get { return GetValue(() => LastName); } set { SetValue(() => LastName, value); } }
public Customer(bool isActive, string firstName, string lastName)
{
this.IsActive = isActive;
this.FirstName = firstName;
this.LastName = lastName;
}
}
public class ActionCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public bool CanExecute(object parameter) { return true; }
public void Execute(object parameter)
{
if (_action != null)
_action();
}
}
}
链接文章中的
PagingCollectionView
未实现IEditableCollectionView
,因此您无法更改网格中的值
不幸的是,WPF中似乎不存在标准的分页集合视图。但是Silverlight的PagedCollectionView
确实有效,可以将代码复制到项目中。我想您也可以安装Silverlight SDK并添加对System.Windows.Data.dll的引用。或者,如果您可以自己实现IEditableCollectionView
,我相信您会找到一些接受者;)
对于看起来像是最佳选择的项目,您将需要来自的所有代码
测试输出:
视图模型:
using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfApplication1.ViewModels
{
public class CustomersViewModel : NotifyBase // replace NotifyBase by implementing INotifyPropertyChanged
{
public PagedCollectionView CustomerCollection { get; private set; }
public int TotalPages { get { return (int)Math.Ceiling((double)CustomerCollection.ItemCount / (double)CustomerCollection.PageSize); } }
public int PageNumber { get { return CustomerCollection.PageIndex + 1; } }
public ICommand MoveNextCommand { get { return GetValue(() => MoveNextCommand); } set { SetValue(() => MoveNextCommand, value); } }
public ICommand MovePreviousCommand { get { return GetValue(() => MovePreviousCommand); } set { SetValue(() => MovePreviousCommand, value); } }
public CustomersViewModel()
{
this.CustomerCollection = new PagedCollectionView(new ObservableCollection<Customer>
{
new Customer(true, "Michael", "Delaney"),
new Customer(false, "James", "Ferguson"),
new Customer(false, "Andrew", "McDonnell"),
new Customer(true, "Sammie", "Hunnery"),
new Customer(true, "Olivia", "Tirolio"),
new Customer(false, "Fran", "Rockwell"),
new Customer(false, "Andrew", "Renard"),
});
this.CustomerCollection.PageSize = 3;
this.MoveNextCommand = new ActionCommand(MoveNext);
this.MovePreviousCommand = new ActionCommand(MovePrevious);
}
private void MoveNext()
{
this.CustomerCollection.MoveToNextPage();
OnPropertyChanged("PageNumber");
}
private void MovePrevious()
{
this.CustomerCollection.MoveToPreviousPage();
OnPropertyChanged("PageNumber");
}
}
public class Customer : NotifyBase // replace NotifyBase by implementing INotifyPropertyChanged
{
public bool IsActive { get { return GetValue(() => IsActive); } set { SetValue(() => IsActive, value); } }
public string FirstName { get { return GetValue(() => FirstName); } set { SetValue(() => FirstName, value); } }
public string LastName { get { return GetValue(() => LastName); } set { SetValue(() => LastName, value); } }
public Customer(bool isActive, string firstName, string lastName)
{
this.IsActive = isActive;
this.FirstName = firstName;
this.LastName = lastName;
}
}
public class ActionCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public bool CanExecute(object parameter) { return true; }
public void Execute(object parameter)
{
if (_action != null)
_action();
}
}
}
链接文章中的
PagingCollectionView
未实现IEditableCollectionView
,因此您无法更改网格中的值
不幸的是,WPF中似乎不存在标准的分页集合视图。但是Silverlight的PagedCollectionView
确实有效,可以将代码复制到项目中。我想您也可以安装Silverlight SDK并添加对System.Windows.Data.dll的引用。或者,如果您可以自己实现IEditableCollectionView
,我相信您会找到一些接受者;)
对于看起来像是最佳选择的项目,您将需要来自的所有代码
测试输出:
视图模型:
using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfApplication1.ViewModels
{
public class CustomersViewModel : NotifyBase // replace NotifyBase by implementing INotifyPropertyChanged
{
public PagedCollectionView CustomerCollection { get; private set; }
public int TotalPages { get { return (int)Math.Ceiling((double)CustomerCollection.ItemCount / (double)CustomerCollection.PageSize); } }
public int PageNumber { get { return CustomerCollection.PageIndex + 1; } }
public ICommand MoveNextCommand { get { return GetValue(() => MoveNextCommand); } set { SetValue(() => MoveNextCommand, value); } }
public ICommand MovePreviousCommand { get { return GetValue(() => MovePreviousCommand); } set { SetValue(() => MovePreviousCommand, value); } }
public CustomersViewModel()
{
this.CustomerCollection = new PagedCollectionView(new ObservableCollection<Customer>
{
new Customer(true, "Michael", "Delaney"),
new Customer(false, "James", "Ferguson"),
new Customer(false, "Andrew", "McDonnell"),
new Customer(true, "Sammie", "Hunnery"),
new Customer(true, "Olivia", "Tirolio"),
new Customer(false, "Fran", "Rockwell"),
new Customer(false, "Andrew", "Renard"),
});
this.CustomerCollection.PageSize = 3;
this.MoveNextCommand = new ActionCommand(MoveNext);
this.MovePreviousCommand = new ActionCommand(MovePrevious);
}
private void MoveNext()
{
this.CustomerCollection.MoveToNextPage();
OnPropertyChanged("PageNumber");
}
private void MovePrevious()
{
this.CustomerCollection.MoveToPreviousPage();
OnPropertyChanged("PageNumber");
}
}
public class Customer : NotifyBase // replace NotifyBase by implementing INotifyPropertyChanged
{
public bool IsActive { get { return GetValue(() => IsActive); } set { SetValue(() => IsActive, value); } }
public string FirstName { get { return GetValue(() => FirstName); } set { SetValue(() => FirstName, value); } }
public string LastName { get { return GetValue(() => LastName); } set { SetValue(() => LastName, value); } }
public Customer(bool isActive, string firstName, string lastName)
{
this.IsActive = isActive;
this.FirstName = firstName;
this.LastName = lastName;
}
}
public class ActionCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public bool CanExecute(object parameter) { return true; }
public void Execute(object parameter)
{
if (_action != null)
_action();
}
}
}
链接文章中的
PagingCollectionView
未实现IEditableCollectionView
,因此您无法更改网格中的值
不幸的是,WPF中似乎不存在标准的分页集合视图。但是Silverlight的PagedCollectionView
确实有效,可以将代码复制到项目中。我想您也可以安装Silverlight SDK并添加对System.Windows.Data.dll的引用。或者,如果您可以自己实现IEditableCollectionView
,我相信您会找到一些接受者;)
对于看起来像是最佳选择的项目,您将需要来自的所有代码
测试输出:
视图模型:
using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfApplication1.ViewModels
{
public class CustomersViewModel : NotifyBase // replace NotifyBase by implementing INotifyPropertyChanged
{
public PagedCollectionView CustomerCollection { get; private set; }
public int TotalPages { get { return (int)Math.Ceiling((double)CustomerCollection.ItemCount / (double)CustomerCollection.PageSize); } }
public int PageNumber { get { return CustomerCollection.PageIndex + 1; } }
public ICommand MoveNextCommand { get { return GetValue(() => MoveNextCommand); } set { SetValue(() => MoveNextCommand, value); } }
public ICommand MovePreviousCommand { get { return GetValue(() => MovePreviousCommand); } set { SetValue(() => MovePreviousCommand, value); } }
public CustomersViewModel()
{
this.CustomerCollection = new PagedCollectionView(new ObservableCollection<Customer>
{
new Customer(true, "Michael", "Delaney"),
new Customer(false, "James", "Ferguson"),
new Customer(false, "Andrew", "McDonnell"),
new Customer(true, "Sammie", "Hunnery"),
new Customer(true, "Olivia", "Tirolio"),
new Customer(false, "Fran", "Rockwell"),
new Customer(false, "Andrew", "Renard"),
});
this.CustomerCollection.PageSize = 3;
this.MoveNextCommand = new ActionCommand(MoveNext);
this.MovePreviousCommand = new ActionCommand(MovePrevious);
}
private void MoveNext()
{
this.CustomerCollection.MoveToNextPage();
OnPropertyChanged("PageNumber");
}
private void MovePrevious()
{
this.CustomerCollection.MoveToPreviousPage();
OnPropertyChanged("PageNumber");
}
}
public class Customer : NotifyBase // replace NotifyBase by implementing INotifyPropertyChanged
{
public bool IsActive { get { return GetValue(() => IsActive); } set { SetValue(() => IsActive, value); } }
public string FirstName { get { return GetValue(() => FirstName); } set { SetValue(() => FirstName, value); } }
public string LastName { get { return GetValue(() => LastName); } set { SetValue(() => LastName, value); } }
public Customer(bool isActive, string firstName, string lastName)
{
this.IsActive = isActive;
this.FirstName = firstName;
this.LastName = lastName;
}
}
public class ActionCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action _action;
public ActionCommand(Action action)
{
_action = action;
}
public bool CanExecute(object parameter) { return true; }
public void Execute(object parameter)
{
if (_action != null)
_action();
}
}
}
似乎是有约束力的问题。您是否可以发布一个可以复制相同内容的应用程序工作示例?为什么不使用ObservableCollection并将网格的DataContext设置为此集合?代码与我在上面发布的URL中的代码相同,唯一的更改是我正在传递网格并添加了一个方法private void RefreshGrid()哪个答案?关于你在这里链接的问题,有两个答案。似乎是有约束力的问题。您是否可以发布一个可以复制相同内容的应用程序工作示例?为什么不使用ObservableCollection并将网格的DataContext设置为此集合?代码与我在上面发布的URL中的代码相同,唯一的更改是我正在传递网格并添加了一个方法private void RefreshGrid()哪个答案?关于你在这里链接的问题,有两个答案。似乎是有约束力的问题。您是否可以发布一个可以复制相同内容的应用程序工作示例?为什么不使用ObservableCollection并将网格的DataContext设置为此集合?代码与我在上面发布的URL中的代码相同,唯一的更改是我正在传递网格并添加了一个方法private void RefreshGrid()哪个答案?关于你在这里链接的问题,有两个答案。似乎是有约束力的问题。您是否可以发布一个可以复制相同内容的应用程序工作示例?为什么不使用ObservableCollection并将网格的DataContext设置为此集合?代码与我在上面发布的URL中的代码相同,唯一的更改是我正在传递网格并添加了一个方法private void RefreshGrid()哪个答案?关于你在这里链接的问题,有两个答案。