private void RefreshEventsForElement(ElementSave selectedElement)
        {
            EventsViewModel viewModel = new EventsViewModel();

            viewModel.InstanceSave = null;
            viewModel.ElementSave = selectedElement;

            mEventsDataGrid.Instance = viewModel;
            mEventsDataGrid.MembersToIgnore.Add("InstanceSave");
            mEventsDataGrid.MembersToIgnore.Add("ElementSave");
            mEventsDataGrid.Categories[0].Name = "Events on this";

            MemberCategory exposed = new MemberCategory();
            exposed.Name = "Exposed";

            var exposedEvents = SelectedState.Self.SelectedElement.Events.Where(item => !string.IsNullOrEmpty(item.ExposedAsName));

            foreach (var eventSave in exposedEvents)
            {
                EventInstanceMember instanceMember = new EventInstanceMember(
                    SelectedState.Self.SelectedElement,
                    SelectedState.Self.SelectedInstance,
                    eventSave);

                var local = eventSave;

                instanceMember.ContextMenuEvents.Add(
                    "Rename",
                    delegate
                    {
                        TextInputWindow tiw = new TextInputWindow();
                        tiw.Message = "Enter new name";
                        tiw.Result = local.ExposedAsName;

                        if (tiw.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            bool isValid = true;

                            // todo:
                            //string whyNotValid = null;
                            //isValid = NameVerifier.Self.IsEventNameValid(tiw.Result, out whyNotValid);

                            if (isValid)
                            {
                                string oldName = local.ExposedAsName;
                                local.ExposedAsName = tiw.Result;
                                RenameManager.Self.HandleRename(selectedElement, local, oldName);
                                GumCommands.Self.FileCommands.TryAutoSaveCurrentElement();
                                GumCommands.Self.GuiCommands.RefreshPropertyGrid();
                            }
                        }

                    });

                exposed.Members.Add(instanceMember);

            }

            mEventsDataGrid.Categories.Add(exposed);
        }
        private void RefreshEventsForInstance(InstanceSave selectedInstance, ElementSave selectedElement)
        {
            var instanceElement = ObjectFinder.Self.GetElementSave(SelectedState.Self.SelectedInstance.BaseType);

            if (instanceElement != null)
            {
                foreach (var eventSaveInBase in instanceElement.Events.Where(item =>
                    item.Enabled
                    && (string.IsNullOrEmpty(item.GetSourceObject()) || !string.IsNullOrEmpty(item.ExposedAsName))
                    ))
                {
                    var eventSave = selectedElement.Events.FirstOrDefault(item => item.Name ==
                        selectedInstance.Name + "." + eventSaveInBase.GetExposedOrRootName());

                    if (eventSave == null)
                    {
                        eventSave = new EventSave();
                        eventSave.Name = selectedInstance.Name + "." + eventSaveInBase.GetExposedOrRootName();
                        // I don't think we want to add this here yet do we?
                        // We should add it only if the user checks it
                        //selectedElement.Events.Add(eventSave);
                    }

                    EventInstanceMember instanceMember = new EventInstanceMember(
                        selectedElement,
                        selectedInstance,
                        eventSave);

                    if (!string.IsNullOrEmpty(eventSave.ExposedAsName))
                    {
                        instanceMember.DisplayName = eventSave.GetRootName() + " (exposed as " +
                            eventSave.ExposedAsName + ")";
                    }

                    mEventsDataGrid.Categories[0].Members.Add(instanceMember);
                }
            }

            // Now loop through all objects and give them an Expose right click option
            foreach (var category in mEventsDataGrid.Categories)
            {
                foreach (var member in category.Members)
                {
                    string rawName = member.Name;
                    if(member.DisplayName.Contains(" (exposed"))
                    {
                        rawName = member.DisplayName.Substring(0, member.DisplayName.IndexOf(" (exposed"));
                    }

                    var eventSave = selectedElement.Events.FirstOrDefault(item => item.Name ==
                        selectedInstance.Name + "." + rawName);

                    bool isAlreadyExposed = eventSave != null && !string.IsNullOrEmpty(eventSave.ExposedAsName);

                    if (isAlreadyExposed)
                    {
                        member.ContextMenuEvents.Add("Un-expose Event", delegate
                            {
                                eventSave.ExposedAsName = null;
                                if (SelectedState.Self.SelectedElement != null)
                                {
                                    GumCommands.Self.FileCommands.TryAutoSaveCurrentElement();
                                    GumCommands.Self.GuiCommands.RefreshPropertyGrid(true);
                                }
                            }
                            );

                    }
                    else
                    {
                        member.ContextMenuEvents.Add("Expose Event", HandleExposeEventClick);
                    }
                }
            }
        }