ContextMenu

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • asharda@woh.rr.com

    ContextMenu

    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.ContextMe nu>
    <ContextMenu Name="cm" StaysOpen="true " Width="Auto"
    Height="Auto" ItemsSource="{B inding Mode=OneWay,
    Source={StaticR esource ListItemsDS}, XPath=/ListboxItems/ListboxItem/
    ScreenName}" MenuItem.Click= "add_Click" >
    </ContextMenu>
    </Grid.ContextMen u>

    The c# code for the even is

    private void add_Click(objec t 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!
  • zacks@construction-imaging.com

    #2
    Re: ContextMenu

    On May 15, 2:24 pm, asha...@woh.rr. com wrote:
    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.ContextMe nu>
                <ContextMenu Name="cm" StaysOpen="true " Width="Auto"
    Height="Auto" ItemsSource="{B inding Mode=OneWay,
    Source={StaticR esource ListItemsDS}, XPath=/ListboxItems/ListboxItem/
    ScreenName}" MenuItem.Click= "add_Click" >
                </ContextMenu>
            </Grid.ContextMen u>
    >
    The c# code for the even is
    >
    private void add_Click(objec t 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.

    Comment

    • asharda@woh.rr.com

      #3
      Re: ContextMenu

      On May 15, 3:09 pm, za...@construct ion-imaging.com wrote:
      On May 15, 2:24 pm, asha...@woh.rr. com wrote:
      >
      >
      >
      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.ContextMe nu>
      <ContextMenu Name="cm" StaysOpen="true " Width="Auto"
      Height="Auto" ItemsSource="{B inding Mode=OneWay,
      Source={StaticR esource ListItemsDS}, XPath=/ListboxItems/ListboxItem/
      ScreenName}" MenuItem.Click= "add_Click" >
      </ContextMenu>
      </Grid.ContextMen u>
      >
      The c# code for the even is
      >
      private void add_Click(objec t 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.
      I have tried it but it gives the 1st item in the items instead of the
      item that was clicked.

      private void add_Click(objec t sender, RoutedEventArgs e)
      {
      MessageBox.Show ("CLICKED " + ((sender as
      ContextMenu).It ems.CurrentItem as XmlElement).Inn erText);
      }

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

      Thanks.

      Comment

      • asharda@woh.rr.com

        #4
        Re: ContextMenu

        On May 15, 3:13 pm, asha...@woh.rr. com wrote:
        On May 15, 3:09 pm, za...@construct ion-imaging.com wrote:
        >
        >
        >
        On May 15, 2:24 pm, asha...@woh.rr. com wrote:
        >
        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.ContextMe nu>
        <ContextMenu Name="cm" StaysOpen="true " Width="Auto"
        Height="Auto" ItemsSource="{B inding Mode=OneWay,
        Source={StaticR esource ListItemsDS}, XPath=/ListboxItems/ListboxItem/
        ScreenName}" MenuItem.Click= "add_Click" >
        </ContextMenu>
        </Grid.ContextMen u>
        >
        The c# code for the even is
        >
        private void add_Click(objec t 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.
        >
        I have tried it but it gives the 1st item in the items instead of the
        item that was clicked.
        >
        private void add_Click(objec t sender, RoutedEventArgs e)
        {
        MessageBox.Show ("CLICKED " + ((sender as
        ContextMenu).It ems.CurrentItem as XmlElement).Inn erText);
        }
        >
        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(objec t sender, RoutedEventArgs e)
        {
        // The orig source is a menu item
        MenuItem mi = e.OriginalSourc e 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="PSDL ogin" NodeName="login ">Login</
        ScreenName>
        </ListboxItem>
        <ListboxItem>
        <ScreenName ClassName="PSDI mage4" NodeName="image 4">Image4</
        ScreenName>
        </ListboxItem>
        <ListboxItems >


        and the XAML for dataprovide in the window.resource s was as follows
        <Window.Resourc es>
        <XmlDataProvide r x:Key="ListItem sDS" Source="ScreenL ist.xml" /
        >
        <DataTemplate x:Key="NameTemp late">
        <TextBlock Text="MenuItem"/>
        </DataTemplate>
        <Window.Resourc es>

        Thanks!

        Comment

        Working...