0

I have this MenuBar in my application:

<Menu Grid.Row="0" Height="22" Name="menu1" HorizontalAlignment="Stretch" VerticalAlignment="Top" >
        <MenuItem Header="File" />
        <MenuItem Header="Youtube">

        </MenuItem>
        <MenuItem Header="Help" />
    </Menu>

And i want to add items to the Youtube MenuItem dynamic ,something like this:

MenuItem menu = (MenuItem)sender;
        ItemCollection items = menu.Items;
        items.Clear();

        if (YouTubeAuth.CreateInstance().IsLogin())
        {
            MenuItem refreshItem = new MenuItem();
            refreshItem.Header = "Refresh";
            refreshItem.Click += DidPressRefresh;
            items.Add(refreshItem);

            MenuItem logouttItem = new MenuItem();
            logouttItem.Header = "Signout";
            logouttItem.Click += DidPressLogout;
            items.Add(logouttItem);
        }
        else
        {
            MenuItem loginItem = new MenuItem();
            loginItem.Header = "Login";
            loginItem.Click += DidPressLogin;
            items.Add(loginItem);
        }

It say if you login show logout and refresh otherwise shot login. I try to add this method to Click="DidPressDeleteAllFavorites" of the Youtube MenuItem but it won't work. Any idea how to fix it? what i do wrong?

YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • 1
    What did not work? Do you receive an error? Have you debugged your code? – bash.d Mar 10 '13 at 14:43
  • In the first time i clicked the YouTube MenuItem nothing happen(But this method call), and from there any time i clicked the YouTube MenuItem the MenuItem is open – YosiFZ Mar 10 '13 at 14:45
  • Is the second part of your code an event-handler? – bash.d Mar 10 '13 at 14:47
  • You shouldn't be creating parts of the UI in procedural code. You have to bind these `Menu`s to `Lists` of `ICommand`s. Please look at [this](http://stackoverflow.com/questions/15209870/dynamically-updating-tabcontrol-content-at-runtime/15210593#15210593) explanation of what a `Menu` really is. – Federico Berasategui Mar 10 '13 at 14:48
  • I try to bind it but i want that every time the MenuItem is click it will Bind. – YosiFZ Mar 10 '13 at 15:50

1 Answers1

1

if you are using the MVVM-pattern

<MenuItem Header="Youtube" ItemsSource="{Binding yourProperty}"/>

if you are working with code-behind

XAML

 <MenuItem Header="Youtube" Name="myYoutube"/>

Code-behind

 myYoutube.ItemsSource=yourMenuItems;

EDIT

the problem in your code is in my opinion you just need to call your Event Code on on startup because your Youtube has no subMenuitem on begin or you could also call UpdateLayout() in your event this could also maybe fix it

Working Example

Codebehind

 public partial class MainWindow : Window
    {
        bool test = false;
        public MainWindow()
        {
            InitializeComponent();

            MenuItem_Click(myYouTube, null);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            var mymenuitem = sender as MenuItem;

            MenuItem menu = (MenuItem)sender;
            ItemCollection items = menu.Items;
            items.Clear();

            if (test)
            {
                MenuItem refreshItem = new MenuItem();
                refreshItem.Header = "Refresh";
                //refreshItem.Click += DidPressRefresh;
                items.Add(refreshItem);

                MenuItem logouttItem = new MenuItem();
                logouttItem.Header = "Signout";
                //logouttItem.Click += DidPressLogout;
                items.Add(logouttItem);


                test = false;
            }
            else
            {
                MenuItem loginItem = new MenuItem();
                loginItem.Header = "Login";
                //loginItem.Click += DidPressLogin;
                items.Add(loginItem);

                test = true;
            }
        }
    }

XAML

 <Menu Height="23" HorizontalAlignment="Left" Margin="84,66,0,0" Name="menu1" VerticalAlignment="Top" Width="200">
            <MenuItem Header="File" />
            <MenuItem Header="Youtube" Name="myYouTube" Click="MenuItem_Click">

            </MenuItem>
            <MenuItem Header="Help" />
        </Menu>
WiiMaxx
  • 5,322
  • 8
  • 51
  • 89
  • This is what i do in the Click Event but in the first time i click the menu won't open – YosiFZ Mar 11 '13 at 12:29
  • i will edit my answer because i thing in know now whats wrong ^^ – WiiMaxx Mar 11 '13 at 12:30
  • I noticed that when i add with Menu item to myYoutube the Click event won't called – YosiFZ Mar 11 '13 at 12:40
  • and this method called in your code every time you click myYouTube MenuItem? – YosiFZ Mar 11 '13 at 12:51
  • `private void MenuItem_Click(object sender, RoutedEventArgs e)` is called everytime you clicked on a child item like `Login` – WiiMaxx Mar 11 '13 at 12:54
  • sry but i don't understand your actually problem. Because i thing it fit's your needs or i'm wrong? – WiiMaxx Mar 11 '13 at 12:57
  • From what i understant the click event won't called after i add menu item to menuitem – YosiFZ Mar 11 '13 at 13:05
  • did you add ` Click="MenuItem_Click"` in your XAML? because in my case it works like i think you need it. `onStartup` and after each `Click` on your `MenuItemChilds` (`Refresh`,`Signout`,`Login`) if you click on `Youtube` nothing will happend what else do you need? – WiiMaxx Mar 11 '13 at 13:11
  • by the way i don't know why `MenuItem_Click` won't get called if you click on just on `Youtube` and not on a `Child` – WiiMaxx Mar 11 '13 at 13:12
  • 1
    Thanks for the help, i used your post to achieve my goal – YosiFZ Mar 11 '13 at 13:28