开发者

WPF, MVVM and combobox

I am having some trouble with my comboBox logic in my viewModel. The viewModel populates the comboBox and the user can select items.

When an item is selected, some editable info appears in the view and they can make changes. Now, I want to prompt the user to save if changes were made and not saved and they are trying to select another item in the drop down (a "Save?" yes/no/cancel messageBox).

Right now I need to remember the previously selected item and handle this myself by resetting the selected item if the user selects cancel. Since the comboBox does not have a PreviewSelectionChanged event, this is the only way I can think of of handling this scenario. It works but it gets a little messy hwen there are multiple comboBoxes, etc.

Am I missing anything or is this开发者_如何学Python just the way it needs to be done?


You essentially have to make a flag in your view model called isDirty or something along those lines.

public class EditorViewModel
{
    private bool _isDirty = false;

    private long _editableProperty;
    public long EditableProperty
    {
        get { return _editableProperty; }
        set
        {
            _editableProperty = value;

            // We've detected a change so mark this view model as dirty.
            _isDirty = true;
        }
    }
}

Note that you will have to have to jump through a few more hoops if you want ensure that the data is in fact different from your original. So say someone accidently adds a space in EditableProperty and removes it your view model will think it's dirty and prompt the user.


The Windows Forms ComboBox provided a SelectionChangeCommitted event, but for the WPF ComboBox control you correct in that there is no event that will notify before the selection change occurs that will provide you with a means of cancelling the event.

If you are going to take a change tracking/editable approach, I would recommend considering implementing IChangeTracking and IEditableObject on the items in your combobox items source.

You will probably have to handle the SelectionChanged event, inspect the removed items to determine if the item that was previously selected was modified and then display a dialog requesting confirmation. If no/cancel was indicated, you can then set the selected index back to that of the previously selected item.


What about making the Editable item a copy of an item instead of the actual item?

So your ViewModel would contain

ObservableCollection<MyModel> ComboBoxItems;
int SelectedComboBoxIndex;
MyModel EditingItem;

Whenever the PropertyChange event occurs on SelectedComboBoxIndex, you check and see if EditingItem is null or not. If it is null, it means you're safe to switch and you set

EditingItem = ComboBoxItem[SelectedComboBoxIndex]).Copy();

If the EditingItem is not null, then you throw up a prompt asking if the user wants to save changes or cancel.

When the user hits Save, it takes the EditingItem and applies the changes to the data store and updates the item in the ComboBoxItems list.

If they hit Cancel, the EditingItem is simply discarded.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜