0

I am looking to have a user definable menu bar that contains a list of "favorite" actions that they can perform. I have a predefined menu of buttons which will perform different actions (open a new tab with content, update a record, etc.). I want the user to be able to select one of this controls, and choose to add it to their "favorite actions" which is a toolbar that will sit at the top of the page (this is currently done with a context menu). Ideally, the user will also be able to reorder this list of favorite actions.

Thus far, I've tried to use an ObservableCollection and List which is bound to a list view to tackle the first part of the problem. on the click method of the context menu, I have the following:

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mnu = sender as MenuItem;
            Button MyButton = null;

            if (mnu != null)
            {
                ContextMenu MyContextMenu = (ContextMenu)mnu.Parent;
                MyButton = MyContextMenu.PlacementTarget as Button;
            }
            dc.menuitems.Add(MyButton);
        }

The list works when I add an object, however it did have issues firing INPC. When using an observable collection, I get the following error:

System.ArgumentException: 'Must disconnect specified child from current parent Visual before attaching to new parent Visual.'

I suspect this may be due to the fact that I'm somehow not creating a copy of the element, but rather reassigning it.

Is my approach the best approach? If so, how do I go about resolving the error that I see? What would be the best way to handle reordering of the items? I haven't been able to find much helpful information on creating such a control.

Once I've resolved this issue, I intend to use either a JSON or XML serializer to store this collection in the user settings to have their favorites stay persistent across application launches. Is this the best way to store this information?

lewpie
  • 99
  • 1
  • 1
  • 7
  • The same `Button` can only appear *once* in the visual tree. Either disconnect it from its current parent or create a copy of it and add the copy to `menuitems`. – mm8 Mar 29 '19 at 13:48
  • What would be the best way to perform an actual copy of it? From what I have seen, I'll effectively have to go through, attribute by attribute, and create a new object programmatically. Is there another way? – lewpie Mar 29 '19 at 14:41
  • Check [this](https://stackoverflow.com/questions/32541/how-can-you-clone-a-wpf-object) out. But do you really need a copy or do you just want to "move" the `Button`? – mm8 Mar 29 '19 at 14:42
  • I do want to have a copy of it. I'd like for the button in the main menu to stay where it is, but for it to also be accessible from the favorites menu. – lewpie Mar 29 '19 at 16:11
  • Then you do want to copy it as you want to display two buttons in two different places, right? – mm8 Apr 01 '19 at 07:40

0 Answers0