XAF Win – Edit Action From Lookup

I got a request for a small feature that I believe it is really handy. The idea is from the LookupListView to have an action (a pencil icon) that when executed open the object DetailView where then it can be modified.

Ok, let’s see the code and the end result:

Code:

public class EditLookupRecordsViewController : ViewController<DetailView>
   {
       Dictionary<LookupPropertyEditor, EventHandler> lookupPropertyEditorEventHandlers;
       Dictionary<LookupEdit, ButtonPressedEventHandler> lookupEditEventHandlers;

       protected override void OnActivated()
       {
           base.OnActivated();

           lookupPropertyEditorEventHandlers = new Dictionary<LookupPropertyEditor, EventHandler>();
           lookupEditEventHandlers = new Dictionary<LookupEdit, ButtonPressedEventHandler>();



           foreach (var item in View.Items.OfType<LookupPropertyEditor>())
           {

               var theItem = item;
               if (item.Control == null)
               {
                   EventHandler<EventArgs> controlCreated = null;

                   controlCreated = (s, e) =>
                   {
                       theItem.ControlCreated -= controlCreated;
                       AddOpenObjectButton(theItem);
                   };

                   item.ControlCreated += controlCreated;
               }
               else
               {
                   AddOpenObjectButton(item);
               }

           }
       }

       private void AddOpenObjectButton(LookupPropertyEditor item)
       {

           Image img = ImageLoader.Instance.GetImageInfo("Editor_Edit").Image;
           var ima = (Image)(new Bitmap(img, new Size(16, 16)));
           var openObjectButton = new EditorButton(ButtonPredefines.Glyph, ima, new SuperToolTip());



           if (item.PropertyValue == null)
           {
               openObjectButton.Enabled = false;
           }

           openObjectButton.Enabled = openObjectButton.Enabled && Frame.GetController<OpenObjectController>().Active.ResultValue;

           item.Control.Properties.Buttons.Insert(0, openObjectButton);
           item.Control.Properties.ActionButtonIndex = item.Control.Properties.Buttons.Count - 1;


           EventHandler controlValueChanged = (s, e) =>
           {
               var openObjectController = this.Frame.GetController<OpenObjectController>();
               openObjectButton.Enabled = openObjectController.OpenObjectAction.Active.ResultValue && item.ControlValue != null;
           };

           item.ControlValueChanged += controlValueChanged;
           lookupPropertyEditorEventHandlers[item] = controlValueChanged;

           ButtonPressedEventHandler buttonClick = (s, e) =>
           {
               if (e.Button == openObjectButton)
               {
                   var openObjectController = this.Frame.GetController<OpenObjectController>();
                   openObjectController.OpenObjectAction.DoExecute();
               }
           };

           item.Control.ButtonClick += buttonClick;
           lookupEditEventHandlers[item.Control] = buttonClick;
       }

       protected override void OnDeactivated()
       {
           foreach (var pair in lookupPropertyEditorEventHandlers)
               pair.Key.ControlValueChanged -= pair.Value;

           lookupPropertyEditorEventHandlers.Clear();


           foreach (var pair in lookupEditEventHandlers)
               pair.Key.ButtonClick -= pair.Value;

           lookupEditEventHandlers.Clear();

           base.OnDeactivated();
       }
   }

End result:

Source Code: https://github.com/jjcolumb/BlogDemo

Until next time, XAF out!

 

Posted in XAF

Leave a Reply

Your email address will not be published. Required fields are marked *