user5349170
user5349170

Reputation: 163

Get clicked MenuItem header value from datagrid context menu in WPF/PowerShell

In a WPF DataGrid via PowerShell, I have added a context menu and MenuItems from an Array. I would like to get the header value of clicked MenuItem to handle the click event further. I had tried add_Click event to the MenuItem object but it does return any value. I'm looking for some ideas to get the Header value in this scenario (or) if there is any different better approach to achieve this goal. Thanks in advance.

[xml]$inputXML=@"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Test"
    Title="MainWindow" Height="275" Width="375">
<Window.InputBindings>
        <KeyBinding x:Name="keyTest" Modifiers="Control" Key="N" Command="{Binding CreateCustomerCommand}" />
</Window.InputBindings>

<Grid>
    <DataGrid x:Name="dg" Margin="5,5,0,0" Height="250" Width="350" ColumnWidth="Auto" AlternationCount="1" IsReadOnly="True" SelectionMode="Extended" SelectionUnit="Cell" Background="White" ClipboardCopyMode="IncludeHeader" > 
        <DataGrid.ContextMenu >
            <ContextMenu x:Name="cxmenu" />
        </DataGrid.ContextMenu>
    </DataGrid>
</Grid>
</Window>
"@

[xml]$XAML = $inputXML

$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)

$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'x:Name')]]") | %{Set-Variable -Name "$($_.Name)" -Value $Window.FindName($_.Name)}

#sample data
$DataSet = New-Object System.Data.DataSet
$Table = $DataSet.Tables.Add("Table")
$Properties = @("Country","Capital","Population")
$Properties | foreach {
 $Column = New-Object System.Data.DataColumn($_)
 $Table.Columns.Add($Column)
 }
$Null=$Table.Rows.Add("USA","Washington, D.C","658,893")
$Null=$Table.Rows.Add("China PR","Beijing","20,693,000")
$Null=$Table.Rows.Add("India","New Delhi","16,787,949") 
$Null=$Table.Rows.Add("Japan","Tokyo","13,189,000")
$Null=$Table.Rows.Add("Philippines","Manila","12,877,253")
$Null=$Table.Rows.Add("Russia","Moscow","11,541,000")
$Null=$Table.Rows.Add("Egypt","Cairo","10,230,350")

#populate datagrid
$DataView = New-Object System.Data.DataView($Table)
$array = New-Object System.Collections.ArrayList
[void] $array.AddRange($DataView)       
$dg.clear()
$dg.ItemsSource = $array
$dg.IsReadOnly = $true

#add MenuItem to context menu
$cxMenuitem = New-Object Windows.Controls.MenuItem
$header = ('Test1','Test2','Test3')
for ($i = 0; $i -le $header.Count -1; $i++)
        {
            $cxMenuitem.Header = [string]$header[$i]
            $cxMenu.Items.Add($cxMenuitem.Header)
        }

$cxMenuitem.Add_Click({    
    #Get the Header of the clicked MenuItem from Context Manu here for further handling
    [System.Windows.MessageBox]::Show($cxMenu.Items)
})

#Display Form
$Window.ShowDialog() | Out-Null

Upvotes: 1

Views: 854

Answers (1)

mm8
mm8

Reputation: 169160

You should be able to cast $args[0] to a MenuItem and access its Header property directly:

$cxMenuitem.Add_Click({    
    $sender = [System.Windows.Controls.MenuItem]$args[0]
    # Use $sender.Header ...
})

Upvotes: 1

Related Questions