当 ICollectionView 排序或筛选时,无法将新项目添加到 Silverlight DataForm
我在 Silverlight 4 页面上有一个 DataForm。我将它绑定到下面类的视图上。我可以使用 DataForm 中内置的控件来添加、删除、编辑和向前/向后移动记录。但是,一旦我删除 Filter 或 SortDescription 的注释,每次按“添加 +”按钮时,我都会收到可怕的“当项目存在验证错误或正在编辑且 AutoCommit 为 false 时无法更改货币”错误。我已经被困在这个问题上几个小时了,没有任何线索。
public class TestData {
OperationsDataContext context;
ICollectionView view;
public ICollectionView View { get { return view; } }
public IEditableCollectionView EditableView { get { return ((IEditableCollectionView)view); } }
public TestData() {
context = new OperationsDataContext();
context.Locations.Add(new Location { LocationId = 1, LocationName = "Home", CreatorUserId = 1 });
context.Locations.Add(new Location { LocationId = 2, LocationName = "Work", CreatorUserId = 1 });
context.Locations.Add(new Location { LocationId = 3, LocationName = "Office", CreatorUserId = 1 });
view = ((ICollectionViewFactory)context.Locations).CreateView();
// View.Filter = (o) => true;
// View.SortDescriptions.Add(new SortDescription("LocationName", ListSortDirection.Ascending));
}
}
我尝试使用代码(而不是 DataForm)手动添加数据,即使指定了过滤器和排序,它也能正常工作。
TestData testData = new TestData();
Location item = testData.EditableView.AddNew() as Location;
testData.EditableView.CommitNew();
为什么它可以通过代码工作而不是通过 DataForm 工作?为什么 DataForm 在未指定过滤器时可以工作,但在指定始终返回 true 的无操作过滤器时会失败?
I've got a DataForm on a Silverlight 4 page. I bind it to the View on the class below. I am able to add, delete, edit, and move forward/back through records just fine using the controls built into the DataForm. But as soon as I remove the comment for the Filter or SortDescription, then every time I press the Add + button I get the dreaded "cannot change currency when an item has validation errors or it is being edited and AutoCommit is false" error. I've been stuck on this for hours and don't have a clue.
public class TestData {
OperationsDataContext context;
ICollectionView view;
public ICollectionView View { get { return view; } }
public IEditableCollectionView EditableView { get { return ((IEditableCollectionView)view); } }
public TestData() {
context = new OperationsDataContext();
context.Locations.Add(new Location { LocationId = 1, LocationName = "Home", CreatorUserId = 1 });
context.Locations.Add(new Location { LocationId = 2, LocationName = "Work", CreatorUserId = 1 });
context.Locations.Add(new Location { LocationId = 3, LocationName = "Office", CreatorUserId = 1 });
view = ((ICollectionViewFactory)context.Locations).CreateView();
// View.Filter = (o) => true;
// View.SortDescriptions.Add(new SortDescription("LocationName", ListSortDirection.Ascending));
}
}
I have attempted to add data manually using code - not the DataForm - and it works just fine even when both a filter and sort are specified.
TestData testData = new TestData();
Location item = testData.EditableView.AddNew() as Location;
testData.EditableView.CommitNew();
Why would it work from code but not via the DataForm? And why does the DataForm work when no filter is specified, but fail when a no-op filter that always returns true is specified?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能您有类似的问题 http://forums.silverlight.net/p/111217/ 250982.aspx 帖子
may be you have similar issue as http://forums.silverlight.net/p/111217/250982.aspx post
好吧,我刚刚遇到了完全相同的问题。就我而言,我使用的是绑定到 DataGrid 和 DataForm 的 DomainCollectionView。显然,这可能会导致问题,因为两个控件都想要管理货币。
解决方案是不将 DataForm 直接绑定到 DomainCollectionView,而是将其绑定到
DomainCollectionView.SourceCollection
属性。这样做的缺点是您必须绑定 DataGrid.SelectedItem 和 DataForm.CurrentItem 以使它们保持同步。我还没有发现这种方法的任何其他问题,但它确实解决了在 DataGrid 中排序后尝试添加新项目时的错误。
请参阅 Jeff Handley 关于此问题的评论: http://jeffhandley.com/archive /2011/08/02/ToolkitAugust2011.aspx
Okay, so I just ran into the exact same issue. In my case, I was using a DomainCollectionView that was bound to both, a DataGrid and a DataForm. Apparently, this can cause issues because both controls want to manage the currency.
The solution was to not bind the DataForm directly to the DomainCollectionView, but instead to bind it to the
DomainCollectionView.SourceCollection
property.The downside to this is that you have to bind
DataGrid.SelectedItem
andDataForm.CurrentItem
to keep them both in sync. I have not found any other issues with this approach, but it definitely solved the error when trying to add a new item after sorting in the DataGrid.See the comment from Jeff Handley about this issue: http://jeffhandley.com/archive/2011/08/02/ToolkitAugust2011.aspx