# Hiccup with WPF/Button events

**URL:** <https://www.ruby-forum.com/t/hiccup-with-wpf-button-events/118505>\
**Category:** IronRuby\
**Created:** [October 27, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/hiccup-with-wpf-button-events/118505 "2007-10-27T23:18:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![1100101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/1/ba9def/32.png) [@1100101](https://www.ruby-forum.com/u/1100101)\
**Post date:** [October 27, 2007, 11:18pm UTC](https://www.ruby-forum.com/t/hiccup-with-wpf-button-events/118505/1 "2007-10-27T23:18:59Z")

</div>

Hi there,

I finally found the time to play around with IronRuby today, so I  
compiled the latest build and tried some of the example code out there.  
When extending some WPF examples though, I ran into a problem with  
Buttons and their assigned events, which looks like a bug to me.

I basically created a simple WPF Window and added several items to it,  
including 2 buttons. Each button has its own event block.  
Unfortunately, only one of the event blocks gets triggered, no matter  
which button I press.

I put together some example code here: [http://pastebin.ca/751924](http://pastebin.ca/751924)

I’ve looked over the my code several times and can’t make out any  
problem with it.  
What am I doing wrong? Am I even doing anything wrong at all?

Thanks for taking a look!

---

<div class="post-metadata">

**Author:** ![1100101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/1/ba9def/32.png) [@1100101](https://www.ruby-forum.com/u/1100101)\
**Post date:** [October 27, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/hiccup-with-wpf-button-events/118505/2 "2007-10-27T23:41:06Z")

</div>

On 10/27/07, Frank A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I basically created a simple WPF Window and added several items to it,  
> including 2 buttons. Each button has its own event block.  
> Unfortunately, only one of the event blocks gets triggered, no matter  
> which button I press.

Interesting. I can reproduce this when I run from a file but not when I  
interactively type the text.

---

<div class="post-metadata">

**Author:** ![1100101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/1/ba9def/32.png) [@1100101](https://www.ruby-forum.com/u/1100101)\
**Post date:** [October 28, 2007, 1:28am UTC](https://www.ruby-forum.com/t/hiccup-with-wpf-button-events/118505/3 "2007-10-28T01:28:37Z")

</div>

On 10/27/07, Frank A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I basically created a simple WPF Window and added several items to it,  
> including 2 buttons. Each button has its own event block.  
> Unfortunately, only one of the event blocks gets triggered, no matter  
> which button I press.

In RubyActionBinder.HookEvent is the following code:

rule.SetTarget(rule.MakeReturn(wiringContext.LanguageContext.Binder,  
Ast.SimpleCallHelper(rule.Parameters[0], addMethod,  
Ast.CodeBlockReference(handler, eventInfo.EventHandlerType))));

The rule is subsequently cached so that all handlers added to  
System.Windows.Controls.Button.Click will get the same rule. The rule  
is  
only parameterized by target event – not by code block – which means  
that  
subsequent code generation results in the same code block getting  
attached  
to the event.

---

<div class="post-metadata">

**Author:** ![1100101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/1/ba9def/32.png) [@1100101](https://www.ruby-forum.com/u/1100101)\
**Post date:** [October 29, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/hiccup-with-wpf-button-events/118505/4 "2007-10-29T23:24:27Z")

</div>

Frank A.:

> But how come it works in the interactive console but not when executed  
> from a file?

We’re trying to track down the cause of this, but we know that we’re not  
building the correct rules for all cases in our ActionBinder. It’s  
likely related to this, but we’re not sure right now.

Thanks,  
-John

---

<div class="post-metadata">

**Author:** ![1100101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/1/ba9def/32.png) [@1100101](https://www.ruby-forum.com/u/1100101)\
**Post date:** [October 29, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/hiccup-with-wpf-button-events/118505/5 "2007-10-29T14:02:54Z")

</div>

Curt H. wrote:

> In RubyActionBinder.HookEvent is the following code:
> 
> rule.SetTarget(rule.MakeReturn(wiringContext.LanguageContext.Binder,  
> Ast.SimpleCallHelper(rule.Parameters[0], addMethod,  
> Ast.CodeBlockReference(handler, eventInfo.EventHandlerType))));
> 
> The rule is subsequently cached so that all handlers added to  
> System.Windows.Controls.Button.Click will get the same rule. The rule  
> is  
> only parameterized by target event – not by code block – which means  
> that  
> subsequent code generation results in the same code block getting  
> attached  
> to the event.

But how come it works in the interactive console but not when executed  
from a file?
