Can't add new item to Silverlight DataForm when ICollectionView is sorted or filtered
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开发者_C百科)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?
may be you have similar issue as http://forums.silverlight.net/p/111217/250982.aspx post
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
and DataForm.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
精彩评论