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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:22:51+00:00 2026-05-31T07:22:51+00:00

I’ve been stuck on this for a while now and I can’t work out

  • 0

I’ve been stuck on this for a while now and I can’t work out why. Basically, I have a ContextMenu with some MenuItem objects in it. I have declared Image objects for the MenuItem.Icon properties. I have Command objects bound to the MenuItems and that all works fine… in particular, when the Command.CanExecute method returns false, the MenuItem is correctly disabled and the MenuItem.Header text is greyed out.

I’ve been trying to set the Image.Opacity of the MenuItem Images to 0.5 when the MenuItem is disabled and this is where the problem is. For some reason, a binding in a DataTrigger in the Image.Style cannot find the MenuItem that I am trying to bind to. I have added a simplified example of my problem below.

<UserControl.Resources>
    <Style x:Key="MenuItemIconStyle" TargetType="{x:Type Image}">
        <Setter Property="Width" Value="16" />
        <Setter Property="Height" Value="16" />
        <Style.Triggers>
            <!--This Binding is not working-->
            <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource 
FindAncestor, AncestorType={x:Type MenuItem}}}" Value="False">
                <Setter Property="Image.Opacity" Value="0.5" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <!--This is all working just fine-->
    <ContextMenu x:Key="ContextMenu" DataContext="{Binding PlacementTarget.Tag, 
RelativeSource={RelativeSource Self}}">
        <MenuItem Header="Open" Command="{Binding Open}" CommandParameter="{Binding 
PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
            <MenuItem.Icon>
                <Image Source="/Application;component/Images/Actions/FolderOpen_16.png" 
Style="{StaticResource MenuItemIconStyle}" />
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
    ...
</UserControl.Resources>

Please note that this example is simplified… there are many MenuItems in my application. I am aware that I could individually name each MenuItem and use ElementName to find them, but there must be a better way.

Any help would be greatly appreciated.

UPDATE >>>

Thanks to punker76’s answer, I realised that all I needed to do was to change the Image Trigger to the following:

<Trigger Property="IsEnabled" Value="False">
    <Setter Property="Opacity" Value="0.5" />
</Trigger>

Instead of trying to bind to the MenuItem.IsEnabled property with a DataTrigger, we can bind to the Image.IsEnabled property directly… this is because the when the MenuItem becomes disabled, it also disables its children. Much simpler!

  • 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-05-31T07:22:52+00:00Added an answer on May 31, 2026 at 7:22 am

    try this one

    <Style x:Key="MenuItemIconStyle" TargetType="{x:Type Image}">
      <Setter Property="Width" Value="16" />
      <Setter Property="Height" Value="16" />
      <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
          <Setter Property="Opacity" Value="0.5" />
        </Trigger>
      </Style.Triggers>
    </Style>
    
    <ContextMenu x:Key="ContextMenu" DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
      <!-- IsEnabled="False" is only for testing (tested with kaxaml) -->    
      <MenuItem IsEnabled="False" Header="Open" Command="{Binding Open}" CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
        <MenuItem.Icon>
          <Image Source="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Disambig-dark.svg/25px-Disambig-dark.svg.png"
                 Style="{StaticResource MenuItemIconStyle}" />
        </MenuItem.Icon>
      </MenuItem>
    </ContextMenu>
    

    EDIT

    here is another solution that works (the button gets the DataContext) with this tip that i found:

    How to Solve Execution Problems of RoutedCommands in a WPF ContextMenu

    The problem was, that the commands could not be
    executed, even if the CommandBinding on the parent window allowed it.
    The reason is, that ContextMenus are separate windows with their own
    VisualTree and LogicalTree. The reason is that the CommandManager
    searches for CommandBindings within the current focus scope. If the
    current focus scope has no command binding, it transfers the focus
    scope to the parent focus scope. The simplest solution is to initially
    set the logical focus of the parent window that is not null. When the
    CommandManager searches for the parent focus scope it finds the window
    and handels the CommandBinding correctly. Another solution is to
    manually bind the CommandTarget to the parent ContextMenu.

    <Window.Resources>
      <Style x:Key="MenuItemIconStyle"
              TargetType="{x:Type Image}">
        <Setter Property="Width"
                Value="16" />
        <Setter Property="Height"
                Value="16" />
        <Style.Triggers>
          <Trigger Property="IsEnabled"
                    Value="False">
            <Setter Property="Opacity"
                    Value="0.5" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </Window.Resources>
    
    <Grid>
    
      <Button Content="With ContextMenu"
              DataContext="{Binding ElementName=window, Path=DataContext}">
        <Button.ContextMenu>
          <ContextMenu>
            <MenuItem Header="Enabled"
                      CommandTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget}"
                      Command="{Binding Open}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
              <MenuItem.Icon>
                <Image Source="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Disambig-dark.svg/25px-Disambig-dark.svg.png"
                        Style="{StaticResource MenuItemIconStyle}" />
              </MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Disabled"
                      CommandTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget}"
                      Command="{Binding NotOpen}"
                      CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
              <MenuItem.Icon>
                <Image Source="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Disambig-dark.svg/25px-Disambig-dark.svg.png"
                        Style="{StaticResource MenuItemIconStyle}" />
              </MenuItem.Icon>
            </MenuItem>
          </ContextMenu>
        </Button.ContextMenu>
      </Button>
    
    </Grid>
    

    code behind

    public partial class Window11 : Window
    {
      public static readonly DependencyProperty OpenProperty =
        DependencyProperty.Register("Open", typeof(ICommand), typeof(Window11), new PropertyMetadata(default(ICommand)));
      public static readonly DependencyProperty NotOpenProperty =
        DependencyProperty.Register("NotOpen", typeof(ICommand), typeof(Window11), new PropertyMetadata(default(ICommand)));
    
      public ICommand NotOpen {
        get { return (ICommand)this.GetValue(NotOpenProperty); }
        set { this.SetValue(NotOpenProperty, value); }
      }
    
      public ICommand Open {
        get { return (ICommand)this.GetValue(OpenProperty); }
        set { this.SetValue(OpenProperty, value); }
      }
    
      public Window11() {
        this.DataContext = this;
    
        this.InitializeComponent();
    
        this.Open = new RoutedCommand("Open", typeof(Window11));
        this.CommandBindings.Add(new CommandBinding(this.Open, null, (sender, args) =>
                                                                        {
                                                                          args.CanExecute = true;
                                                                        }));
        this.NotOpen = new RoutedCommand("NotOpen", typeof(Window11));
        this.CommandBindings.Add(new CommandBinding(this.NotOpen, null, (sender, args) =>
                                                                          {
                                                                            args.CanExecute = false;
                                                                          }));
      }
    }
    

    hope this works

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have this code to decode numeric html entities to the UTF8 equivalent character.
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.