In the previous post I presented an extension method used mostly for overriding the edit and delete commands of a collection. One may ask “why do I want to do this?”. Apart from any other requirements/business logic dependent reason one might want to implement, for me there is one simple yet important reason: I don’t like at all (to be kind) the default add/edit modal windows when adding or editing an entry. It’s not a coincidence that the
FIRST sample I wrote for LightSwitch and posted in the Samples of msdn.com/lightswitch was a set of extension methods and contracts to easily replace standard modal windows with custom ones.
Most of the times when I have an editable grid screen, selecting Add or Edit I DON’T want the modal window to pop-up, I just want to edit in the grid. Or in list and details screen I want to edit the new or existing entry in the detail part of the screen.
This is the main reason I most of the times override the default Add/Edit command behavior. And for this reason I created and use the next two extension methods:
public static void AddFocus<T>(this VisualCollection<T> collection, string focusControl, bool inCollection)
where T : class, IEntityObject {
collection.AddNew();
if (focusControl != null) {
try {
if (inCollection)
collection.Screen.FindControlInCollection(focusControl, collection.SelectedItem).Focus();
else
collection.Screen.FindControl(focusControl).Focus();
}
catch {
collection.EditSelected();
}
}
}
public static void EditFocus<T>(this VisualCollection<T> collection, string focusControl, bool inCollection)
where T : class, IEntityObject {
if (focusControl != null) {
try {
if (inCollection)
collection.Screen.FindControlInCollection(focusControl, collection.SelectedItem).Focus();
else
collection.Screen.FindControl(focusControl).Focus();
}
catch {
collection.EditSelected();
}
}
}
So what you have to do in your MyCollectionAddAndEditNew_Execute partial method is write something like:
MyCollection.AddFocus("TheNameOfTheControlYouWantToFocus", true);
or for MyCollectionEdit_Execute partial method:
MyCollection.EditFocus("TheNameOfTheControlYouWantToFocus", true);
if you have a list detail screen the same code applies but with inCollection = false.
One could implement one method for collections and one for details. Also notice
where constraints applied to
T generic parameter. The
class constraint is required for declaring
VisualCollection<T>. The
IEntityObject constraint is required so that we avoid the arbitrary casting of
collection.SelectedItem to
IEntityObject so as to be passed to
FindControlInCollection as second parameter.
Notice that in the extension methods I don’t check if the control name exists. I just use try catch. On error (if the control name is not valid) I choose to fall-back to the default behavior. You can change this without any intellectual rights being violated
.