How to Setup a Routed Event in WPF ?

Using (or providing) Microsoft.NET Classes

How to Setup a Routed Event in WPF ?

Postby PGilbert on Thu Aug 26, 2010 9:27 pm

If I define a variable named 'XamlString' with the following XAML code:

Code: Select all
<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  SizeToContent="WidthAndHeight">
  <Border
    Height="50"
    Width="300"
    Margin="10"
    BorderBrush="Gray"
    BorderThickness="1">
    <StackPanel Background="LightGray" Orientation="Horizontal">
      <Button
        Name="YesButton"
        Width="30"
        Content="Yes"/>
      <Button
        Name="NoButton"
        Width="30"
        Content="No"/>
      <Button
        Name="CancelButton"
        Width="60"
        Content="Cancel"/>
    </StackPanel>
  </Border>
</Window>


than with the following line I can get the Window Root Object:

RootObj←XamlReader.Load ⎕NEW XmlTextReader(⎕NEW StringReader(⊂XamlString))

and doing:

RootObj.Show

I get the following window with 3 buttons:

YesNoCancel.png
YesNoCancel.png (11.41 KiB) Viewed 5346 times


I understand that if I want to add a Click event on the 'YesButton' I need to do simply:

YesButton ← RootObj.FindName ⊂'YesButton'
YesButton.onClick ← 'YesButton_Click'


My question is: what do I need to do to be able to define a 'Routed Event' on the 'Window' object that will catch all the click events of all the buttons as describe here

RootObj.Button.onClick ← 'CommonClickHandler' ⍝ Does not work

Thanks in advance,

Pierre Gilbert
User avatar
PGilbert
 
Posts: 419
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: How to Setup a Routed Event in WPF ?

Postby MikeHughes on Fri Aug 27, 2010 1:00 pm

Hi Pierre

Try

{}EventManager.RegisterRoutedEvent 'Click'RoutingStrategy.Bubble RoutedEventHandler Window
re←EventManager.RoutedEvent
i←re.Name⍳⊂'Click'
RootObj.AddHandler (i⊃re)(⎕new RoutedEventHandler (⎕or 'CommonClickHandler'))


Michael
User avatar
MikeHughes
 
Posts: 86
Joined: Thu Nov 26, 2009 9:03 am
Location: Market Harborough, Leicestershire, UK

Re: How to Setup a Routed Event in WPF ?

Postby PGilbert on Fri Aug 27, 2010 5:10 pm

Many Thanks Mike for your answer and the off forum discussions, here is what works for me:

RootObj←XamlReader.Load ⎕NEW XmlTextReader(⎕NEW StringReader(⊂XamlString))

∇CommonClickHandler[⎕]∇
[0] CommonClickHandler ra
[1] (2⊃ra).Source.Name

{}EventManager.RegisterRoutedEvent 'ClickEvent' RoutingStrategy.Bubble RoutedEventHandler Window

re←EventManager.GetRoutedEvents

index←re.Name⍳⊂'Click'

RootObj.AddHandler (index⊃re) (⎕NEW RoutedEventHandler(⎕OR 'CommonClickHandler'))

RootObj.Show

When you click on any of the buttons you can obtain its name and take action accordingly.

Pretty neat again Dyalog. Congratulations.

Pierre Gilbert
User avatar
PGilbert
 
Posts: 419
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada


Return to Microsoft.NET

Who is online

Users browsing this forum: No registered users and 1 guest