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 97715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:03:28+00:00 2026-05-11T00:03:28+00:00

I wan’t to hide/show menuitems in a contextmenu using some propery of databound object.

  • 0

I wan’t to hide/show menuitems in a contextmenu using some propery of databound object. But my menuitems don’t hide, they behave as if their Visiblity would be set to Visibility.Hidden (not Visibility.Collapsed as it really is), what’s the reason of such behaviour?

Here’s an example:

XAML:

<Window x:Class='MenuTest.Window1'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'     xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     Title='Window'>     <Window.Resources>         <BooleanToVisibilityConverter x:Key='converter'/>         <DataTemplate x:Key='template'>                         <MenuItem Visibility='{Binding Visible, Converter={StaticResource converter}}' Header='{Binding Title}' />         </DataTemplate>         <ContextMenu x:Key='menu' ItemTemplate='{StaticResource template}'/>                         </Window.Resources>     <Grid>         <Button VerticalAlignment='Center' HorizontalAlignment='Center' Click='OnClick'>Button</Button>     </Grid> </Window> 

And code behind:

public partial class Window1 : Window     {         public ObservableCollection<Item> list = new ObservableCollection<Item>();         public Window1()         {             InitializeComponent();             list.Add(new Item() { Title = 'First', Visible = true }); ;             list.Add(new Item() { Title = 'Second', Visible = false }); ;             list.Add(new Item() { Title = 'Third', Visible = false }); ;             list.Add(new Item() { Title = 'Fourth', Visible = true }); ;         }          private void OnClick(object sender, RoutedEventArgs e)         {             ContextMenu cm =  FindResource('menu') as ContextMenu;             cm.PlacementTarget = e.OriginalSource as UIElement;             cm.Placement = System.Windows.Controls.Primitives.PlacementMode.Left;             cm.ItemsSource = list;             cm.IsOpen = true;         }     }      public class Item     {         public string Title { get; set; }         public bool Visible { get; set; }     } 

The result is menu with four items (but two in the middle without any visible text in header).

  • 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. 2026-05-11T00:03:28+00:00Added an answer on May 11, 2026 at 12:03 am

    This behavior is because the ItemTemplate of ContextMenu will be applied to each of your bound Items within the MenuItem created for that Item. By putting a MenuItem into your DataTemplate, you create a nested MenuItem. Even if you collapse the inner one, since the outer one is still visible, there is still space for it.

    You can fix this by getting rid of the nested MenuItem in your DataTemplate and replacing it with a TextBlock, and using a Style that applies to MenuItem to set the Visibility:

    <Window x:Class='MenuTest.Window1'         xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'         xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'         Title='Window'>     <Window.Resources>         <BooleanToVisibilityConverter x:Key='converter'/>         <DataTemplate x:Key='template'>             <TextBlock Text='{Binding Title}'/>         </DataTemplate>         <ContextMenu x:Key='menu' ItemTemplate='{StaticResource template}'>             <ContextMenu.Resources>                 <Style TargetType='{x:Type MenuItem}'>                     <Setter Property='Visibility' Value='{Binding Visible, Converter={StaticResource converter}}'/>                 </Style>             </ContextMenu.Resources>         </ContextMenu>     </Window.Resources>     <Grid>         <Button HorizontalAlignment='Center' VerticalAlignment='Center' Click='OnClick'>Button</Button>     </Grid> </Window> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wan't to use some C source in my Objective-c proj but the source
I wan to hide/show my caller id from my activity programmatically. I tried to
How wan i display text in an OpenGL program? I'm using MinGW, but the
I wan't to load some data from my XML file using this function: public
I wan't in my application ComboBox with list of colors, but I don't won't
I wan't to do this using Mono, but Mono is so transparent the question
I wan't to get data's from CallLog.Calls.CONTENT_URI in non Activity classes. Am using cursor
i wan to disable drag event using jquery and want to bind one another
I wan to get the value of only one week. I am using the
I wan't to use this controll in my app but cant find which it

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.