views:

109

answers:

2

I want to make an extension method which fills a stackpanel with buttons.

In order to do this I have to pass in a mouse-click-handler.

What type does the mouseClickHandler parameter have to be here?

I know it's something like these but they all don't work:

  • delegate
  • Func<object, RoutedEventArgs>
  • Action<>

Code:

public static void FillMenuWithPages(this StackPanel spMenu, string menuIdCode, 
            PagingManager pagingManager, ??????? mouseClickHandler)
{
    var menuPageItems = PageItems.GetPageItemsWithMenu(menuIdCode);
    spMenu.Children.Clear();
    foreach (var pageItem in menuPageItems)
    {
        var button = new Button();
        button.Content = pageItem.Title;
        button.Margin = new Thickness { Right = 3 };
        button.Tag = pageItem.IdCode;
        button.Cursor = Cursors.Hand;
        button.Click += new RoutedEventHandler(mouseClickHandler);
        if (pageItem.IdCode == pagingManager.CurrentPageItemIdCode)
            button.IsEnabled = false;
        spMenu.Children.Add(button);
    }
}

I would call it like this:

QuickMenu.FillMenuWithPages("quick", PagingManager, Menu_Button_Click);

and this is the mouse click hander:

void Menu_Button_Click(object sender, RoutedEventArgs e)
{
    string pageItemIdCode = ((FrameworkElement)sender).Tag.ToString();
    PagingManager.SwitchPage(pageItemIdCode);
}
+1  A: 

Pass it as a RoutedEventHandler like so:

public static void FillMenuWithPages(this StackPanel spMenu, string menuIdCode,
            PagingManager pagingManager, RoutedEventHandler mouseClickHandler)
Adam Maras
perfect, that's what I need, thanks
Edward Tanguay
A: 

It depends on whether it's a WPF or Winforms application. For WPF it would be RoutedEventHandler (as mentioned in the ButtonBase.Click documentation). For winforms, it's EventHandler (as seen in the Control.Click documentation).

Fredrik Mörk