Skip to main content

Microsoft Silverlight

Event Routing and events on buttons inside a ListBoxRSS Feed

(0)

trentnix
trentnix

Member

Member

128 points

32 Posts

Event Routing and events on buttons inside a ListBox

I am creating a set of buttons inside of a ListBox and am having trouble dynamically adding event handlers to deal with a situation where a button in the list has been clicked.  Here's the setup:

I have a list box, let's call it _listBox and I have a set of classes called ButtonA, ButtonB, and ButtonC.  These classes inherit from a base type of ButtonBase, which inherits from the Button control.  I add these buttons dynamically to the listbox like so:

ButtonA a = new ButtonA();
ButtonB b = new ButtonB();
ButtonC c = new ButtonC();

_listBox.Items.Add(a);
_listBox.Items.Add(b);
_listBox.Items.Add(c);

_listBox.Height = a.Height + b.Height + c.Height;

Everything renders more or less like I expect it to.  Within the constructor for ButtonBase (the class each button inherits from) I attach some event to the LeftMouseButtonUp event:

MouseLeftButtonUp += new MouseButtonEventHandler(DoAction);

And DoAction has a signature of:

public void DoAction(object o, MouseButtonEventArgs e)

But every time I click the button, this event is not fired.  I tried overriding the Click function in the base Button class and got the same result.  I have verified that the constructor for ButtonBase is firing and that the line that adds the DoAction event handler to the MouseLeftButtonUp event does execute.

Help!

coder21
coder21

Member

Member

132 points

46 Posts

Re: Event Routing and events on buttons inside a ListBox

this post should help

http://leeontech.wordpress.com/2008/03/11/adding-event-handlers-to-buttons-inside-a-listbox/

trentnix
trentnix

Member

Member

128 points

32 Posts

Re: Event Routing and events on buttons inside a ListBox

Sorry I couldn't really follow what was being explained there.  Why isn't the event being routed as I'm expecting?

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities