开发者

Silverlight dataform currentitem issue

I have a page with two controls on it, a datagrid and a dataform.

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

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

My problem is, when setting the dataform's currenitem property, if I use just the PagedCollectionView (view) without .CurrentItem, I lose the validation on the dataform. All the required fields are not seen as required. If I use the pcv.CurrentItem as my dataform's CurrentItem validation works fine, but then another issue arrises.

When I use the PagedCollectionView's current item as the dataform's current item:

A user selects an item in the datagrid and the object is loaded fine in the dataform. If a user changes a certain value in any of the textfields on the dataform and then selects a different item to load the dataform with, the following error is thrown:

"Cannot change currency when an item has validation errors or it is being edited and AutoCommit is false. Set ItemsSource to a ICollectionView to manage currency instead." I am not using the paging properties of the dataform and I have my own save button on the form.

I would appreciate any help, this is my first silverlight project that I 开发者_开发技巧am working on.

Edit- I used dataform.CommitEdit when changing the dataform's currentitem. One thing that this did not resolve is if there is a validation error on the form, the currency error is thrown. Is there anyway to bypass this. AutoEdit is true and AutoCommit is false for the dataform


It's a bit hard to determine exactly what's going on here without a sample, but here's an observation that may help resolve the problem. Try instead to bind the ItemsSource property of both the DataGrid and the DataForm to the collection view, and don't bind the DataForm's CurrentItem property. They're magically kept in sync (the selected item in the DataGrid will set the current item in the DataForm) - this is a feature of the CollectionView. This may or may not solve your problem, but either way it won't hurt :).

Blatant self promotion: this and other features of the CollectionView are covered in my book Pro Business Applications with Silverlight 4 :).


I had this problem a lot of times. And always in case add new item. After few frustrating days I downloaded source codes of Silverlight toolkit. (You could find in Programs FIles directory (Mine were is C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Source) ) Compile and reference instead of assembly 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 conditional:

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

And DataForm started work fine. Without exception and mistakes.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜