ContextMenu

A

asharda

Hi,

I am trying to create a context menu in my application. The context
menu takes menu otems from an XML file. In XAML the code is

<Grid.ContextMenu>
<ContextMenu Name="cm" StaysOpen="true" Width="Auto"
Height="Auto" ItemsSource="{Binding Mode=OneWay,
Source={StaticResource ListItemsDS}, XPath=/ListboxItems/ListboxItem/
ScreenName}" MenuItem.Click="add_Click">
</ContextMenu>
</Grid.ContextMenu>

The c# code for the even is

private void add_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("CLICKED " );
}

I am unable to get the value of the clicked item.

Any idea what I am doing wrong?

Thanks for your help in advance!
 
Z

zacks

Hi,

I am trying to create a context menu in my application. The context
menu takes menu otems from an XML file. In XAML the code is

<Grid.ContextMenu>
            <ContextMenu Name="cm" StaysOpen="true" Width="Auto"
Height="Auto" ItemsSource="{Binding Mode=OneWay,
Source={StaticResource ListItemsDS}, XPath=/ListboxItems/ListboxItem/
ScreenName}" MenuItem.Click="add_Click">
            </ContextMenu>
        </Grid.ContextMenu>

The c# code for the even is

private void add_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("CLICKED " );
        }

I am unable to get the value of the clicked item.

Any idea what I am doing wrong?

Thanks for your help in advance!

Have you tried casting the sender as a local ContextMenu object?

btw, I think RoutedEventArgs should be EventArgs for a ContextMenu
OnClick event handler.
 
A

asharda

Have you tried casting the sender as a local ContextMenu object?

btw, I think RoutedEventArgs should be EventArgs for a ContextMenu
OnClick event handler.

I have tried it but it gives the 1st item in the items instead of the
item that was clicked.

private void add_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("CLICKED " + ((sender as
ContextMenu).Items.CurrentItem as XmlElement).InnerText);
}

Even if you use e.Source instead of sender it still gives the 1st
item.

Thanks.
 
A

asharda

I have tried it but it gives the 1st item in the items instead of the
item that was clicked.

private void add_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("CLICKED " + ((sender as
ContextMenu).Items.CurrentItem as XmlElement).InnerText);
}

Even if you use e.Source instead of sender it still gives the 1st
item.

Thanks.

Ok! I managed to figure out the problem and hence thought of posting
it here

the add_click method had to be slightly modified.

private void add_Click(object sender, RoutedEventArgs e)
{
// The orig source is a menu item
MenuItem mi = e.OriginalSource as MenuItem;
// The header is an XmlElement
XmlElement att = mi.Header as XmlElement;
MessageBox.Show(att.InnerText);

}

Where my xml looked like
<ListboxItems xmlns="">

<ListboxItem>
<ScreenName ClassName="PSDLogin" NodeName="login">Login</
ScreenName>
</ListboxItem>
<ListboxItem>
<ScreenName ClassName="PSDImage4" NodeName="image4">Image4</
ScreenName>
</ListboxItem>
<ListboxItems>


and the XAML for dataprovide in the window.resources was as follows
<DataTemplate x:Key="NameTemplate">
<TextBlock Text="MenuItem"/>
</DataTemplate>
<Window.Resources>

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top