how to call a function from a button in an itemtemplate?

Jun 18, 2011 at 4:24 PM

Calling a function from a button's On_Click event is fine for "normal" buttons, but what is the syntax to call a function from a button contained in an item template? I've done a few hours of research on Google studying this, the closet I've seen in general WPF posts involves using XAML to set both the Command and CommandParameter attributes on the button. Are there any examples I may have overlooked for binding a function to a button inside an itemtemplate using ShowUI?

The code below shows various attemtps - all of which fail. I've tried plenty of others, too, looking for the magic incantation.

ipmo ShowUI

$CommandStrings = @{Cmd1 = "select 1";Cmd2 = "select 2"}
   
function a_btn_Refresh($sender, $e)  {
# Set a breakpoint on the following line
    $thisbtn = $sender
}

# This example fails to hit the breakpoint in the function above
New-Window -Title T1  {
  Listbox  -ItemsSource { $CommandStrings } -ItemTemplate {
    StackPanel -HorizontalAlignment "Left" -VerticalAlignment "Top" -Orientation "Horizontal" -Name "c_sp" {
      Label -HorizontalAlignment "Left" -VerticalAlignment "Center" -Name "c_lbl_coName"
      Button  -Name "c_btn_update"  -On_Click {&a_btn_Refresh($this, $args)}
        #-On_Click ({[scriptblock] {a_btn_Refresh($this, $args)}}) # various tries
        #-On_Click ({return &a_btn_Refresh($this, $args)})
        #-On_Click ({return a_btn_Refresh($this, $args)})
        #-On_Click ({a_btn_Refresh($this, $args)})
    } | ConvertTo-DataTemplate   -binding @{
        "c_lbl_coName.Content" = "Key"
        "c_btn_update.Content" = "Value"
    }
  }
} -show

# This example will hit the breakpoint when the buttons are pressed
New-Window -Title 'T1'  {
  stackpanel -HorizontalAlignment "Left" -VerticalAlignment "Top" {
    Button  "First Button"  -width 60 -Name btn_update  -On_Click {&a_btn_Refresh($this, $args)} # What is the magic variable for event arguments? it's not $args...
    Button  "Second Button" -width 60  -Name btn_update  -On_Click {&a_btn_Refresh($this, $args)}
  }
} -show

Jun 19, 2011 at 6:03 PM

Got it. Not sure if there is a way to declare the on-click event handler in the button declaration contained in itemtemplate declaration (I wissh there was a way - I think it would look cleaner), but I found that you can add the on-click event handler to every button when the listbox is rendered, like so.

ipmo ShowUI

$CommandStrings = @{Cmd1 = "select 1";Cmd2 = "select 2"}
   
function a_btn_Refresh($sender, $e)  {
# Set a breakpoint on the following line
    $thisbtn = $sender
}

# This example wilkl hit the breakpoint whn a button is pressed, and $sender in the function will be the button object which was pressed
New-Window -Title T1  {
  Listbox  -ItemsSource { $CommandStrings } -ItemTemplate {
    StackPanel -HorizontalAlignment "Left" -VerticalAlignment "Top" -Orientation "Horizontal" -Name "c_sp" {
      Label -HorizontalAlignment "Left" -VerticalAlignment "Center" -Name "c_lbl_coName"
      Button  -Name "c_btn_update"
    } | ConvertTo-DataTemplate   -binding @{
        "c_lbl_coName.Content" = "Key"
        "c_btn_update.Content" = "Value"
    }
  } -On_Loaded {
   Get-ChildControl c_btn_update |% {Add-EventHandler $_ Click { &a_btn_Refresh($_)}}
   #Get-ChildControl c_btn_update |% {write-debug ('in listboxloaded {0} has resources {1} which has EventHandlers {2} which has keys {3}' -f $_.Name, $_.Resources, $_.Resources.EventHandlers, ([string]::Join(",", $_.Resources.EventHandlers.Keys)))}
   }
} -show

Coordinator
Jun 20, 2011 at 5:33 AM

You need Command Binding ... let me see if I can put together an example in the morning.