Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Disabling MenuItem Icon

Disabling MenuItem Icon

Scheduled Pinned Locked Moved WCF and WF
helpcsswpfwcfdata-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    D Dubya
    wrote on last edited by
    #1

    Hi all, For the last couple of days I've been try to disable the MenuItem.Icon image when the MenuItem itself becomes disabled. I've implemented Style Triggers for my Toolbar icons but I can't seem to get them to work with the MenuItem icons. This is my current style definition (in my Main Menu uc):

    <UserControl.Resources>
    <!-- The following style causes the toolbar buttons to become dimmed when they are disabled -->
    <Style TargetType="{x:Type Image}" x:Key="menuImageStyle">
    <Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
    <Setter Property="Opacity" Value="0.4" />
    </DataTrigger>
    </Style.Triggers>
    </Style>
    </UserControl.Resources>

    With this as the MenuItem itself:

    <Grid>
    <Menu Height="22" Name="mnuMainMenu">
    <MenuItem Name="itmFile" Header="{x:Static r:ucMainMenu.itmFile}">
    <MenuItem Name="itmSave" Header="{x:Static r:ucMainMenu.itmSave}" Command="ApplicationCommands.Save" InputGestureText="Ctrl+S">
    <MenuItem.Icon>
    <Image Name="imgSave" Source="/SCPS;component/Icons/save.ico" Height="16" Style="{StaticResource menuImageStyle}" />
    </MenuItem.Icon>
    </MenuItem>
    </MenuItem>
    </Menu>
    </Grid>

    I found a blog posting that recommended moving farther up the element tree by using UIElement instead of MenuItem as the Ancestor Type, but that didn't work either. I did notice the following being displayed in the Output window:
    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.MenuItem', AncestorLevel='1''. BindingExpression:Path=IsEnabled; DataItem=null; target element is 'Image' (Name='imgSave'); target property is 'NoTarget' (type 'Object')
    Also, in trying to debug this issue, I got myself a copy of Snoop and noticed that the root element of a Context Menu is not the ApplicationRoot... Unsure as to whether or not this relates to my problem... Any thoughts?

    I 1 Reply Last reply
    0
    • D D Dubya

      Hi all, For the last couple of days I've been try to disable the MenuItem.Icon image when the MenuItem itself becomes disabled. I've implemented Style Triggers for my Toolbar icons but I can't seem to get them to work with the MenuItem icons. This is my current style definition (in my Main Menu uc):

      <UserControl.Resources>
      <!-- The following style causes the toolbar buttons to become dimmed when they are disabled -->
      <Style TargetType="{x:Type Image}" x:Key="menuImageStyle">
      <Style.Triggers>
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type MenuItem}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
      <Setter Property="Opacity" Value="0.4" />
      </DataTrigger>
      </Style.Triggers>
      </Style>
      </UserControl.Resources>

      With this as the MenuItem itself:

      <Grid>
      <Menu Height="22" Name="mnuMainMenu">
      <MenuItem Name="itmFile" Header="{x:Static r:ucMainMenu.itmFile}">
      <MenuItem Name="itmSave" Header="{x:Static r:ucMainMenu.itmSave}" Command="ApplicationCommands.Save" InputGestureText="Ctrl+S">
      <MenuItem.Icon>
      <Image Name="imgSave" Source="/SCPS;component/Icons/save.ico" Height="16" Style="{StaticResource menuImageStyle}" />
      </MenuItem.Icon>
      </MenuItem>
      </MenuItem>
      </Menu>
      </Grid>

      I found a blog posting that recommended moving farther up the element tree by using UIElement instead of MenuItem as the Ancestor Type, but that didn't work either. I did notice the following being displayed in the Output window:
      System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.MenuItem', AncestorLevel='1''. BindingExpression:Path=IsEnabled; DataItem=null; target element is 'Image' (Name='imgSave'); target property is 'NoTarget' (type 'Object')
      Also, in trying to debug this issue, I got myself a copy of Snoop and noticed that the root element of a Context Menu is not the ApplicationRoot... Unsure as to whether or not this relates to my problem... Any thoughts?

      I Offline
      I Offline
      Insincere Dave
      wrote on last edited by
      #2

      Use {DyamicResource menuImageStyle} instead.

      D 1 Reply Last reply
      0
      • I Insincere Dave

        Use {DyamicResource menuImageStyle} instead.

        D Offline
        D Offline
        D Dubya
        wrote on last edited by
        #3

        Great!.. Works like a charm. Do I need to use DynamicResource because the 'inner' MenuItems aren't compiled at the same time as the rest of my XAML?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups