views:

156

answers:

1

Hi %username%,

I wonder how you do such thing. Assume, we have MVVM CRUD app which modifies a tree (menu structure, for example). We have a view model with the menu items and two views: the first with a TreeView and the second with a DataForm. Main problems are:

  1. DataForm can not handle hierarchical data.
  2. Depending on the menu item selected in the TreeView the DataForm should display different set of fields (for example for menu items with children or without).

I've ended up with the following. View model has 3 fields:

  1. Items — the collection of MenuItem objects which have their own Children collection for building hierarchical data source.
  2. SelectedItem — currently selected MenuItem in the TreeView.
  3. EditedItemEditViewModel object which basically has two descendants: MenuItemEditViewModel and LeafMenuItemEditViewModel. This property is set automatically when SelectedItem is changed. Its actual type is inferred from the SelectedItem.Children emptiness.

TreeView is bound to Items and SelectedItem. DataForm is not required to maintain currency in this case (instead current item is set by the TreeView) nor it is responsible for creating and deleting items. That's why I decided to bind only its CurrentItem to view model's EditedItem (ItemsSource is unbound). Its AutoCommit is set to False (when it is True and ItemsSource is unbound all current item changes get copied to newly selected item when you select different item in the TreeView, which is not so nice). DataForm fields are autogenerated.

Obviously, that now if we select an item in the TreeView, then make some changes in the DataForm and try to select different item in the TreeView we'll get well-known

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

In this case I want DataForm to discard all changes implicitly. There is a workaround to call DataForm.CancelEdit() before TreeView selected item is changed (usually an event like PreviewSelectionChanged or BeforeSelectionChanged). But it is not the MVVM way since the TreeView and the DataForm are defined in completely different views (read: is not acceptable).

Is there something like AutoCancel which forces DataForm to cancel changes when its CurrentItem is changed? Maybe someone from dev team can answer? Or how would you deal with such problem?

A: 

Hello,

Have you tried to set AutoCommit at True ?

Jmix90
Yep, it's not a solution. I mentioned it: "when it is True and ItemsSource is unbound all current item changes get copied to newly selected item when you select different item in the TreeView, which is not so nice"
Denis Parchenko