Current Silverlight Data Problem

I have a page with two controls, datagrid and data format.

In a datagrid, I have a list of all the objects of a particular class. When the user selects an item in a datagrid, the data form is loaded with the selected object.

dataForm.CurrentItem = view.CurrentItem; view is a PagedCollectionView that contains only the selected item.

My problem is that when setting the currenitem property of the data form, if I use only the PagedCollectionView (view) without .CurrentItem, I lose validation in the data form. All required fields are not considered as required. If I use pcv.CurrentItem as my data form, then checking CurrentItem works fine, but then another problem occurs.

When I use the current PagedCollectionView element as the current data element:

The user selects an element in a datagrid, and the object is loaded in the data form accurately. If the user changes a specific value in any of the text fields of the data form and then selects another element to load the data form, the following error occurs:

"It is not possible to change the currency if the item has validation errors or it is being edited and AutoCommit is false. Set the ItemsSource to ICollectionView to manage the currency instead." I do not use the search properties of the data form, and I have my own save button in the form.

I would be grateful for any help, this is my first Silverlight project that I am working on.

Change I used dataform.CommitEdit when changing the current value of dataform. One thing that this did not allow is that if there is a validation error in the form, a currency error occurs. In any case, to get around this. AutoEdit is true and AutoCommit is false for the data form

+4
source share
4 answers

It's a little difficult to pinpoint what happens here without a sample, but there is a comment here that can help solve the problem. Instead, try to bind the ItemsSource property of both the DataGrid and the DataForm to the collection view and not bind the DataForm CurrentItem property. They are magically synchronized (the selected item in the DataGrid will set the current item in the DataForm) - this is the CollectionView function. This may or may not solve your problem, but in any case it will not hurt :).

Malicious Self-Assessment: This and other CollectionView features are described in my Pro Business Applications book with Silverlight 4 :).

+4
source

I have had this problem many times. And always in case of adding a new element. After a few disappointing days, I downloaded the Silverlight toolkit source code. (You can find it in the Programles directory (Mine are C: \ Program Files (x86) \ Microsoft SDK \ Silverlight \ v4.0 \ Toolkit \ Apr10 \ Source)) Compile and link instead of building System.Windows.Controls.Data.DataForm .Toolkit

In debug mode, we see strange behavior in DataForm.cs:

private static void OnCurrentItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DataForm dataForm = d as DataForm; if (dataForm != null && !dataForm.AreHandlersSuspended()) { if (dataForm._lastItem != null && dataForm.ShouldValidateOnCurrencyChange) { dataForm.ValidateItem(); } if ((!dataForm.AutoCommitPreventsCurrentItemChange && dataForm.IsItemValid) && (e.NewValue == null || dataForm._collectionView == null || dataForm._collectionView.Contains(dataForm.CurrentItem) )) { dataForm.SetUpNewCurrentItem(); dataForm.GenerateUI(true /* clearEntityErrors */, true /* swapOldAndNew */); dataForm.UpdateCurrentItem(); SetAllCanPropertiesAndUpdate(dataForm, false /* onlyUpdateStates */); dataForm._lastItem = dataForm.CurrentItem; dataForm.OnCurrentItemChanged(EventArgs.Empty); } else { dataForm.SetValueNoCallback(e.Property, e.OldValue); throw new InvalidOperationException(string.Format(Globalization.CultureInfo.InvariantCulture, System.Windows.Controls.Data.DataForm.Toolkit.Resources.DataForm_CannotChangeCurrency, "AutoCommit", "ItemsSource", "ICollectionView")); } } } 

dataForm._collectionView.Contains (dataForm.CurrentItem) returns false, even the same object exists in dataForm._collectionView

I changed the conditional:

 if ((!dataForm.AutoCommitPreventsCurrentItemChange && dataForm.IsItemValid) && (e.NewValue == null || dataForm._collectionView == null || dataForm._collectionView.Contains(dataForm.CurrentItem) || dataForm.CurrentItem == e.NewValue )) 

And the DataForm started working fine. Without exception and errors.

+1
source
 private void DataForm_EditEnding(object sender, DataFormEditEndingEventArgs e) { if (e.EditAction == DataFormEditAction.Commit) { ... } else { DataForm1.ValidationSummary.Errors.Clear(); } } 
0
source

Check for any validation error when you bind the current element, if you have any then clear them BindingItem.ValidationErrors.Clear (); then bind the item to the data form.

0
source

All Articles