How do I...Set a Click Event on a Button

Using Microsoft Windows Presentation Foundation and Syncfusion WPF Libraries

How do I...Set a Click Event on a Button

Postby Fiona|Dyalog on Wed Jul 30, 2014 10:42 am

In APL:

      ⎕USING←'' 'System.Windows,WPF/PresentationFramework.DLL'
⎕USING,←⊂'System.Windows.Controls'
btn←⎕NEW Button
btn #.WPF.Event.Add 'Click' 'foo'
foo ⍝ Add lists all handlers as its result


In XAML:

Code: Select all
xmlns:apl="clr-namespace:MJHSoftware;assembly=MJHSoftware.APLExtension"
<Button Click="{apl:Event foo}" />


where function foo has the form:

Code: Select all
   ∇ foo(Sender e)
[1]  e.Handled←1            ⍝ Marks the event as "handled"
   ∇


To add a second handler (cumulatively):

      btn #.WPF.Event.Add 'Click' 'goo'
foo goo ⍝ Add lists all handlers as its result
User avatar
Fiona|Dyalog
 
Posts: 56
Joined: Mon Apr 22, 2013 12:59 pm

Re: How do I...Set a Click Event on a Button

Postby norbertjurkiewicz84 on Wed Nov 26, 2014 7:01 pm

Where can I find the appropriate MJHSoftware.APLExtension assembly to make the XAML example work?
(It's the little things that make the difference :-)
User avatar
norbertjurkiewicz84
 
Posts: 55
Joined: Mon Nov 01, 2010 7:26 pm

Re: How do I...Set a Click Event on a Button

Postby norbertjurkiewicz84 on Thu Dec 04, 2014 7:00 pm

Can anyone point me to directions for setting commands and events in XAML? The example below shows using another assembly. Is that something that's available and ready for public release?
(It's the little things that make the difference :-)
User avatar
norbertjurkiewicz84
 
Posts: 55
Joined: Mon Nov 01, 2010 7:26 pm

Re: How do I...Set a Click Event on a Button

Postby Vince|Dyalog on Fri Dec 05, 2014 10:15 am

Hi Norbert,

The mjh software extension might not be needed anymore for this.

Please have a look at the supplied wpfintro workspace in the ws subdirectory of your 14.0 install and "Chapter 4: Windows Presentation Foundation" of the Dyalog .NET Interface Guide.

Regards,

Vince
Vince|Dyalog
 
Posts: 312
Joined: Wed Oct 01, 2008 9:39 am

Re: How do I...Set a Click Event on a Button

Postby norbertjurkiewicz84 on Fri Dec 05, 2014 3:48 pm

Hi Vince,

Both the documentation and wpfintro show how to use workspace functions to find individual objects and assign callbacks in code. I'd like to assign the callbacks and commands directly in XAML.

I looked through the wpfintro code and didn't see any examples of callback assignment in XAML. Please correct me if I glossed over it by mistake.


.NET Ch. 4 and wpfintro style.

Code: Select all
[13] mnuFahrenheit←win.FindName⊂'mnuFahrenheit'
[14] mnuFahrenheit.onClick←'SET_F'
[15] mnuCentigrade←win.FindName⊂'mnuCentigrade'
[16] mnuCentigrade.onClick←'SET_C'
[17] (btnF2C←win.FindName⊂'btnF2C').onClick←'f2c'
[18] (btnC2F←win.FindName⊂'btnC2F').onClick←'c2f'
[19] (btnQuit←win.FindName⊂'btnQuit').onClick←'Quit'
[20] (scrTemp←win.FindName⊂'scrTemp').onScroll←'F2C'


vs example in this thread.

Code: Select all
<Button Click="{apl:Event foo}" />



I'd like to do the latter.



Norbert.
(It's the little things that make the difference :-)
User avatar
norbertjurkiewicz84
 
Posts: 55
Joined: Mon Nov 01, 2010 7:26 pm

Re: How do I...Set a Click Event on a Button

Postby Dick Bowman on Fri Dec 05, 2014 5:16 pm

The issue - as I understand it, and someone will surely be along soon to correct me - is that XAML doesn't know about APL, and the code that gets called when you load XAML into your APL session can't figure out what you're talking about.

There are clever ways to resolve this.

There are also really dumb ways to resolve this.

As an accredited simpleton, I do things the dumb way...

First step is to establish a convention that you'll use in your XAML - what I do is to stick a ∇ in front of the callback name, so the XAML contains something like this...

Click="∇#.HTMLTOOLS.VIEW.View"

Then my application has stuff like this...

#.wdwDogMetal names callbacks←#.UTIL.XAML.LoadXAML(#.GLOBALS.homefolder,'XAML/htmltools.xaml')(⊂'Ribbon')
#.wdwDogMetal←names #.UTIL.WPFTOOLS.SetObjects #.wdwDogMetal
#.wdwDogMetal←callbacks #.UTIL.WPFTOOLS.SetCallbacks #.wdwDogMetal

where <LoadXAML> <SetObjects> and <SetCallbacks> are utility functions what I wrote (and or copied from the better-informed). I won't show them because they do a bit more than the bare bones - but basically <LoadXAML> strips out the bits that "real XAML" can't understand and passes the essential on, while spewing out lists of names and callbacks - your APL needs the names anyway, so <SetObjects> sorts all that out, while <SetCallbacks> does what it says on the can.

The point being that - having invested the time once to create these ustilities - I don't have to think about how they're doing whatever they do, I just use them in my thought-free world.

As I say - there are cleverer ways to go about this. But as my old auntie used to say, "great thing about APL is that there are so many ways to skin a cat - and it doesn't cost you a lot to try several of them out".
Visit http://apl.dickbowman.com to read more from Dick Bowman
User avatar
Dick Bowman
 
Posts: 235
Joined: Thu Jun 18, 2009 4:55 pm

Re: How do I...Set a Click Event on a Button

Postby PGilbert on Fri Dec 05, 2014 6:20 pm

Hello Norbert, find attached a copy of the function we use to 'Fix' our Xaml (GetObjectFromXaml). In our case the callback function need to begin with the underscore character '_' because it is compatible with Visual Studio (VS does not accept a del '∇' character as first character, but will accept an underscore '_'). Also in order to fix a callback the control need to be named. For example this will work in Xaml:

<Button x:Name="MyButton" Click="_buttonClick"/>

The function 'GetObjectFromXaml' will only fix the names and events of the controls. If you need to fix only the names, the function 'FixSimpleXaml' is shorter.

Pierre Gilbert

GetObjectFromXaml.txt
(5.71 KiB) Downloaded 318 times

FixSimpleXaml.txt
(556 Bytes) Downloaded 339 times
User avatar
PGilbert
 
Posts: 362
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada


Return to WPF & Syncfusion

Who is online

Users browsing this forum: No registered users and 1 guest