Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:53:47+00:00 2026-06-08T17:53:47+00:00

I am writing a Silverlight for Windows Phone 7.5 app. I want to reference

  • 0

I am writing a Silverlight for Windows Phone 7.5 app.

I want to reference the ContextMenu inside my LongListSelector because I want to set .IsOpen to false as soon as the ContextMenu Click event is called. My thought was that this should happen automatically but it does not.

One of my MenuItem’s sets the visibility of a <Grid> from collapsed to visible which mimics a PopUp. Whilst the code executes fine and the Visibility does indeed change. The UI of the app does not show the Grid unless the ContextMenu closes.

My XAML of the LongListSelector which contains a ContextMenu called Menu that I wish to reference in the ContextMenuItem Click event.

 <toolkit:LongListSelector x:Name="moviesLongList" Background="Transparent" IsFlatList="False" GroupHeaderTemplate="{StaticResource GroupHeaderTemplate}" GroupItemTemplate="{StaticResource GroupItemTemplate}" SelectionChanged="moviesLongList_SelectionChanged" GroupViewClosing="moviesLongList_GroupViewClosing" GroupViewOpened="moviesLongList_GroupViewOpened">

                    <toolkit:LongListSelector.GroupItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel/>
                        </ItemsPanelTemplate>
                    </toolkit:LongListSelector.GroupItemsPanel>

                    <toolkit:LongListSelector.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Height="91" Margin="20,0,0,20" Orientation="Horizontal">
                                <toolkit:ContextMenuService.ContextMenu >
                                    <toolkit:ContextMenu x:Name="Menu" Opened="ContextMenu_Opened" Loaded="Menu_Loaded" Unloaded="Menu_Unloaded">
                                        <toolkit:ContextMenu.ItemTemplate>
                                            <DataTemplate>
                                                 <toolkit:MenuItem Header="{Binding}" Click="ContextMenuButton_Click" LostFocus="MenuItem_LostFocus" />
                                            </DataTemplate>
                                        </toolkit:ContextMenu.ItemTemplate>
                                    </toolkit:ContextMenu>
                                </toolkit:ContextMenuService.ContextMenu>

                                <Border HorizontalAlignment="Left" Width="61" Height="91" Background="{Binding ID, Converter={StaticResource ThumbImageConvert}}" />
                                <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Width="395">
                                    <TextBlock x:Name="titleTextBox" Text="{Binding Title, Converter={StaticResource TitleConvert}}" Margin="6,0,6,0" d:LayoutOverrides="Width" FontSize="{StaticResource PhoneFontSizeLarge}" VerticalAlignment="Top" HorizontalAlignment="Left"/>
                                    <TextBlock x:Name="yearTextBox" Text="{Binding Year}" Margin="12,0,0,0" HorizontalAlignment="Left" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="{StaticResource PhoneSubtleBrush}" />
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </toolkit:LongListSelector.ItemTemplate>

                </toolkit:LongListSelector>

My code behind ContextMenuItem Click event

  private void ContextMenuButton_Click(object sender, RoutedEventArgs e)
  {
        //
        // This is where I want to set Menu.IsOpen = false to close the ContextMenu.
        //

        if ((sender as MenuItem).Header.ToString() == "lend movie")
        {
                DisableAppBarIcons();
                LendPopUpOverlay.Visibility = System.Windows.Visibility.Visible;

        }

        if ((sender as MenuItem).Header.ToString() == "return to collection")
        {
            ... Do stuff
        }

        if ((sender as MenuItem).Header.ToString() == "add to boxset")
        {
             ... Do stuff

        }

        if ((sender as MenuItem).Header.ToString() == "delete")
        {
            ... Do stuff
        }

   }

I set the ItemSource of the ContextMenu in the ContextMenu_Opened event. The fields are both of type List<String>.

private void ContextMenu_Opened(object sender, RoutedEventArgs e)
    {
        LentMovieObj = (sender as ContextMenu).DataContext as Movies;

        if (LentMovieObj.IsLent)
        {
            (sender as ContextMenu).ItemsSource = menuItemsReturn;
        }
        else
        {
            (sender as ContextMenu).ItemsSource = menuItemsLendOut;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:53:48+00:00Added an answer on June 8, 2026 at 5:53 pm

    Not sure why the ContextMenu is not closing, but here are two solutions. The first is to get the parent of the MenuItem.

    private T GetParentOfType<T>(DependencyObject obj) where T : class
    {
        if (obj == null) return null;
    
        var parent = VisualTreeHelper.GetParent(obj);
        while (parent != null)
        {
            if (parent is T) return parent as T;
    
            parent = VisualTreeHelper.GetParent(parent);
        }
    
        return null;
    }
    

    Then get the Menu from your click handler

    var menu = GetParentOfType<ContextMenu>(sender as MenuItem);
    menu.IsOpen = false;
    

    The second solution is to bind IsOpen to a backing viewmodel

    <toolkit:ContextMenuService.ContextMenu >
        <toolkit:ContextMenu x:Name="Menu" Opened="ContextMenu_Opened" Loaded="Menu_Loaded" Unloaded="Menu_Unloaded" IsOpen="{Binding IsOpen}" ItemsSource="{Binding Items}">
            <toolkit:ContextMenu.ItemTemplate>
                <DataTemplate>
                    <toolkit:MenuItem Header="{Binding}" Click="ContextMenuButton_Click" LostFocus="MenuItem_LostFocus" />
                 </DataTemplate>
            </toolkit:ContextMenu.ItemTemplate>
         </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
    

    Change your open event:

    private void ContextMenu_Opened(object sender, RoutedEventArgs e)
    {
        LentMovieObj = (sender as ContextMenu).DataContext as Movies;
    
        if (LentMovieObj.IsLent)
        {
            (sender as ContextMenu).DataContext = new ContextMenuViewModel(menuItemsReturn);
        }
        else
        {
            (sender as ContextMenu).DataContext = ContextMenuViewModel(menuItemsLendOut);
        }
    }
    

    Then a viewmodel

    public class ContextMenuViewModel : INotifyPropertyChanged
    {
        private bool _isOpen = true;
    
        public ContextMenuViewModel(IEnumerable<string> items)
        {
            Items = items;
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public bool IsOpen
        {
            get { return _isOpen; }
            set { _isOpen = value; OnPropertyChanged("IsOpen"); }
        }
    
        public IEnumerable<String> Items { get; set; }
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    Then set the IsOpen property of your ViewModel to false;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Silverlight pivot app in VS2010 for Windows Phone. I just added
I am writing a Silverlight for Windows Phone (SDK 7.1) app and I am
I writing wp7 silverlight app. I have a listbox with items. I want each
I am writing a Windows 7 Phone App on Visual Studio Express, and I
I'm writing an app for the Windows Phone 7 and although I think I
I'm writing a windows phone 7 app. I have fatal exception handling code where
I'm writing an OOB app using Silverlight 5 and I want to be able
When writing a Silverlight app hooked up to a WCF Web Service, the only
I'm writing a silverlight application that resembles a shopping cart system. This app can
I'm writing a silverlight app which does some real-time charting. Basically, I just have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.