I'm working on a WPF app using MVVM where I'm reusing a view model for viewing/creating/editing/copying a business object (called Flow). There are around 25 fields on the view and some of them need to be disabled depending on whether the user is viewing/editing/... the flow. In an attempt to keep the view and the view model clean I've come up with the following solution:
- I use an enum GuiAction that contains all the different
GuiAction
s (FlowView, FlowEdit, etc..) - I have a property on the Flow view model of type GuiAction that represents whether we're viewing/editing/..
All the fields on the flow have their
IsEnabled
attribute databound to the GuiAction property with a converterControlAvailability
taking the name of the property as a parameter:<CheckBox IsEnabled="{Binding GuiAction, Converter={StaticResource ControlAvailability}, ConverterParameter=Runnable}"></CheckBox>
ControlAvailability
receives both the GuiAction and the parameter name and from that it should return true or false, enabling or disabling the control.
As for the logic in ControlAvailability
, my initial idea to look up availability was to use a two-tier switch statement like so:
public object Convert(object value, ..., object parameter, ...)
{
GuiAction guiAction = (GuiAction)value;
string control = (string)parameter;
switch (guiAction)
{
case GuiAction.FlowView:
switch (control)
{
case "Runnable":
return false;
break;
case "Path":
return false;
break;
...
}
break;
case GuiAction.FlowEdit:
switch (control)
{
case "Runnable":
return true;
break;
...
}
break;
...
}
}
but with 7 GuiActions and 25 controls this will turn into hundreds of lines of code.. I feel that there has to be a better way to do this.. probably in a flat file somewhere.
So.. Is the massive switch statement the way to go or is there a better solution?