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();
collection.EditFocus(focusControl, inCollection);
}
public static void EditFocus<T>(this VisualCollection<T> collection, string focusControl, bool inCollection)
where T : class, IEntityObject {
if (focusControl != null) {
try {
IContentItemProxy control = null;
if (inCollection)
control = collection.Screen.FindControlInCollection(focusControl, collection.SelectedItem);
else
control = collection.Screen.FindControl(focusControl);
Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() => {
try {
control.Focus();
}
catch {
collection.EditSelected();
}
});
}
catch {
if (collection.HasSelection())
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