lundi 6 décembre 2021

I'm having an issue implementing the Command Processor pattern using MVVM for WPF

I'm trying to implement the Command Processor pattern using MVVM for WPF. The Command Processor pattern that I'm roughly following is the one described in the book Pattern-Oriented Software Architecture (Volume 1):

The Command Processor design pattern separates the request for a service from its execution. A command processor component manages requests as separate objects, schedules their execution, and provides additional services such as the storing of request objects for later undo.

My implementation requires that all possible UI actions that may be undoable be stored as commands. This is mostly compatible with WPF's Commands, except for TextBoxes. TextBoxes in WPF don't provide a Command (unlike Buttons and CheckBoxes).

I thought I solved this by adding a TextChangedCommand that executes when the TextChanged event is raised, like this:

<l:CommandTextBox
    Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
    TextChangedCommand="{Binding SetNameCommand, Mode=OneWay}"
    />

But the problem is that the TextChangedCommand is executed not only when the user makes a change but whenever the data-bound property is changed, which happens when undoing the change. In an undo operation, I just want the property to be set to what it was previously, not run a command again.

It's like there needs to be a TextChangedByUserCommand, but before I try to implement this I'm wondering if I'm going about this the wrong way, or if someone has come up with a cleaner solution.

Aucun commentaire:

Enregistrer un commentaire