QueryableCollectionView with datagrid and dataform - selection changed issue

本小妞迷上赌 提交于 2021-02-08 05:41:23

问题


I have an mvvm solution. In my viewModel I have an QueryableCollectionView of the class BaseClass objects and an standalone BaseClass element(let's call it seletedElem).

The BaseClass have a private QueryableCollection of the ChildClass objects - SCollection, and a property that returns this collection. Both classes are implementing the following interfaces: INotifyPropertyChanged, IDataErrorInfo, IEditableObject. In my view I have raddatagrid which itemssource is binded to the ObservableCollection of BaseClass objects from viewModel and selected item is binded to the standalone BaseClass object - selectedElem.

The Dataform is binded to the currently selected grid item in this way :

ItemsSource="{Binding ElementName=xxx,Path=DataContext.seletedElem.SCollection}"

Then before the first DataGrid's selection changed event, or after the new item is inserted to the grid, when I press add button of the raddataform the current item of the SCollection is always an empty item of type ChildClass, but after the grid's selection changed event(when one of the existing items is selected) the current item of the SCollection is always null and I'm unable to edit it's properties.

The dataGrid and dataform are Telerik components.

Is there anyway to fix this behavior?


回答1:


I have found two workarounds how to handle with this situation, but they are not solving the root issue. I will type them here, they may be useful for someone.

1st Copy the QueryableCollection SCollection direct to he view ViewModel and then is case events like grid's SelectionChanged or DataForm's CommitEdit,EndEdit, CancelEdit just set the SCollection of the selected "BaseClass" instance to the current SCollection from the ViewModel

2nd Use the AddingNewItem,EditEnded,DeletingItem DataForm methods to support yor own operations logic esspecialy CRUD and validation operations. So for example in AddingNewItem add manually empty ChildClass instance to the selected object SCollection collecion - code sample :

public void OnAddingNewItem(object sender, AddingNewItemEventArgs e)
        {
            ChildClass item = new ChildClass();
            var queryable = SelectedBaseObject.SCollection.OfType<ChildClass>().ToList();

            queryable.Add(item);

            SelectedBaseObject.SCollection = new QueryableCollectionView(new ObservableCollection<ChildClass>(queryable));


            ((RadDataForm) sender).BeginEdit();
            e.Cancel = true;
        }


来源:https://stackoverflow.com/questions/17695228/queryablecollectionview-with-datagrid-and-dataform-selection-changed-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!