Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto cancel changes in DataForm when selection is changed
    text
    copied!<p>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 <code>TreeView</code> and the second with a <code>DataForm</code>. Main problems are:</p> <ol> <li><code>DataForm</code> can not handle hierarchical data.</li> <li>Depending on the menu item selected in the <code>TreeView</code> the <code>DataForm</code> should display different set of fields (for example for menu items with children or without).</li> </ol> <p>I've ended up with the following. View model has 3 fields:</p> <ol> <li><code>Items</code> — the collection of <code>MenuItem</code> objects which have their own <code>Children</code> collection for building hierarchical data source.</li> <li><code>SelectedItem</code> — currently selected <code>MenuItem</code> in the <code>TreeView</code>.</li> <li><code>EditedItem</code> — <code>EditViewModel</code> object which basically has two descendants: <code>MenuItemEditViewModel</code> and <code>LeafMenuItemEditViewModel</code>. This property is set automatically when <code>SelectedItem</code> is changed. Its actual type is inferred from the <code>SelectedItem.Children</code> emptiness.</li> </ol> <p><code>TreeView</code> is bound to <code>Items</code> and <code>SelectedItem</code>. <code>DataForm</code> is not required to maintain currency in this case (instead current item is set by the <code>TreeView</code>) nor it is responsible for creating and deleting items. That's why I decided to bind only its <code>CurrentItem</code> to view model's <code>EditedItem</code> (<code>ItemsSource</code> is unbound). Its <code>AutoCommit</code> is set to <code>False</code> (when it is <code>True</code> and <code>ItemsSource</code> is unbound all current item changes get copied to newly selected item when you select different item in the <code>TreeView</code>, which is not so nice). <code>DataForm</code> fields are autogenerated.</p> <p>Obviously, that now if we select an item in the <code>TreeView</code>, then make some changes in the <code>DataForm</code> and try to select different item in the <code>TreeView</code> we'll get well-known</p> <blockquote> <p>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</p> </blockquote> <p>In this case I want <code>DataForm</code> to discard all changes implicitly. There is a workaround to call <code>DataForm.CancelEdit()</code> before <code>TreeView</code> selected item is changed (usually an event like <code>PreviewSelectionChanged</code> or <code>BeforeSelectionChanged</code>). But it is not the MVVM way since the <code>TreeView</code> and the <code>DataForm</code> are defined in completely different views (read: is not acceptable).</p> <p>Is there something like AutoCancel which forces <code>DataForm</code> to cancel changes when its <code>CurrentItem</code> is changed? Maybe someone from dev team can answer? Or how would you deal with such problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload