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. WPF
  4. MenuButton CanExecute Firing Wrong

MenuButton CanExecute Firing Wrong

Scheduled Pinned Locked Moved WPF
wpfwcf
11 Posts 5 Posters 5 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.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a MenuButton on my Main Window toolbar:

    Here's the DropDown button code:

    public class DropDownButton : ToggleButton
    {
    public DropDownButton()
    {
    // Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property
    Binding binding = new Binding("Menu.IsOpen");
    binding.Source = this;
    this.SetBinding(IsCheckedProperty, binding);
    DataContextChanged += (sender, args) =>
    {
    if (Menu != null)
    Menu.DataContext = DataContext;
    };
    }

    public ContextMenu Menu
    {
        get { return (ContextMenu)GetValue(MenuProperty); }
        set { SetValue(MenuProperty, value); }
    }
    public static readonly DependencyProperty MenuProperty = DependencyProperty.Register("Menu", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null, OnMenuChanged));
    
    private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var dropDownButton = (DropDownButton)d;
        var contextMenu = (ContextMenu)e.NewValue;
        contextMenu.DataContext = dropDownButton.DataContext;
    }
    
    protected override void OnClick()
    {
    
    L M Richard DeemingR A 4 Replies Last reply
    0
    • K Kevin Marois

      I have a MenuButton on my Main Window toolbar:

      Here's the DropDown button code:

      public class DropDownButton : ToggleButton
      {
      public DropDownButton()
      {
      // Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property
      Binding binding = new Binding("Menu.IsOpen");
      binding.Source = this;
      this.SetBinding(IsCheckedProperty, binding);
      DataContextChanged += (sender, args) =>
      {
      if (Menu != null)
      Menu.DataContext = DataContext;
      };
      }

      public ContextMenu Menu
      {
          get { return (ContextMenu)GetValue(MenuProperty); }
          set { SetValue(MenuProperty, value); }
      }
      public static readonly DependencyProperty MenuProperty = DependencyProperty.Register("Menu", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null, OnMenuChanged));
      
      private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          var dropDownButton = (DropDownButton)d;
          var contextMenu = (ContextMenu)e.NewValue;
          contextMenu.DataContext = dropDownButton.DataContext;
      }
      
      protected override void OnClick()
      {
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      And that's why I use user controls and "code-behind". Ship now or ship maybe. Why a "binding" instead of just a menu command parameter "constant" or "enum"? How many ways can you "add a company"?

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      K 1 Reply Last reply
      0
      • L Lost User

        And that's why I use user controls and "code-behind". Ship now or ship maybe. Why a "binding" instead of just a menu command parameter "constant" or "enum"? How many ways can you "add a company"?

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Gerry Schmitz wrote:

        And that's why I use user controls and "code-behind". Ship now or ship maybe.

        Not sure what that means

        Gerry Schmitz wrote:

        Why a "binding" instead of just a menu command parameter "constant" or "enum"? How many ways can you "add a company"?

        Binding? As opposed to....? Not sure what this response means. How else would this be done? I'm using a command that bound to the menu bars. If I understand what you're asking, I decided to use one command for all menu bars. This way I don't have to have a command and Execute/CanExecute for each bar.

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        L 1 Reply Last reply
        0
        • K Kevin Marois

          Gerry Schmitz wrote:

          And that's why I use user controls and "code-behind". Ship now or ship maybe.

          Not sure what that means

          Gerry Schmitz wrote:

          Why a "binding" instead of just a menu command parameter "constant" or "enum"? How many ways can you "add a company"?

          Binding? As opposed to....? Not sure what this response means. How else would this be done? I'm using a command that bound to the menu bars. If I understand what you're asking, I decided to use one command for all menu bars. This way I don't have to have a command and Execute/CanExecute for each bar.

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          For example:

          CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
          CommandParameter="{x:Static local:ActionId.NewCompany}"
          Tag="company"
          Header="New Company" />

          And "Tag" is your parameter. How many do you need? Or, menu click "sender" identifies the menu item. Your could even examine the "header"; or the "Name". A lot of "binding" for nothing, and only confusing for the next person.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • K Kevin Marois

            I have a MenuButton on my Main Window toolbar:

            Here's the DropDown button code:

            public class DropDownButton : ToggleButton
            {
            public DropDownButton()
            {
            // Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property
            Binding binding = new Binding("Menu.IsOpen");
            binding.Source = this;
            this.SetBinding(IsCheckedProperty, binding);
            DataContextChanged += (sender, args) =>
            {
            if (Menu != null)
            Menu.DataContext = DataContext;
            };
            }

            public ContextMenu Menu
            {
                get { return (ContextMenu)GetValue(MenuProperty); }
                set { SetValue(MenuProperty, value); }
            }
            public static readonly DependencyProperty MenuProperty = DependencyProperty.Register("Menu", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null, OnMenuChanged));
            
            private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var dropDownButton = (DropDownButton)d;
                var contextMenu = (ContextMenu)e.NewValue;
                contextMenu.DataContext = dropDownButton.DataContext;
            }
            
            protected override void OnClick()
            {
            
            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Why not pass the "tag" value as a commandparameter? Seems to me you should be using the enabled property when constructing the menu to manage access via the app security not the canexecute which logically only fires when the item is clicked!

            Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

            K 1 Reply Last reply
            0
            • M Mycroft Holmes

              Why not pass the "tag" value as a commandparameter? Seems to me you should be using the enabled property when constructing the menu to manage access via the app security not the canexecute which logically only fires when the item is clicked!

              Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

              K Offline
              K Offline
              Kevin Marois
              wrote on last edited by
              #6

              Can execute fires alot, like when the window refreshes. I want the b as r disabled BEFORE the user can click on it

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              M 1 Reply Last reply
              0
              • K Kevin Marois

                Can execute fires alot, like when the window refreshes. I want the b as r disabled BEFORE the user can click on it

                If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                Your canexecute defines if the user can execute the menu item based on the users security profile (or am I missing something), why are you not disabling the menu item when you construct the menu, or don't even add the item if the user does not have permissions.

                Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                K 1 Reply Last reply
                0
                • M Mycroft Holmes

                  Your canexecute defines if the user can execute the menu item based on the users security profile (or am I missing something), why are you not disabling the menu item when you construct the menu, or don't even add the item if the user does not have permissions.

                  Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                  K Offline
                  K Offline
                  Kevin Marois
                  wrote on last edited by
                  #8

                  Constructing the menu happens once. The security profile can change at runtime, for example, the admin can grant access. I could force the user to log out and back in, but then I'd have to rebuild the UI to enable/disable things in response

                  If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                  M 1 Reply Last reply
                  0
                  • K Kevin Marois

                    Constructing the menu happens once. The security profile can change at runtime, for example, the admin can grant access. I could force the user to log out and back in, but then I'd have to rebuild the UI to enable/disable things in response

                    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

                    M Offline
                    M Offline
                    Mycroft Holmes
                    wrote on last edited by
                    #9

                    Kevin Marois wrote:

                    The security profile can change at runtime, for example, the admin can grant access.

                    I have supplied a function to refresh/change the user profile from the main landing page (mainwindow) without having to log off. I always use the constructor and disable items. So I'm no use to you with the CanExecute issue.

                    Kevin Marois wrote:

                    I'd have to rebuild the UI to enable/disable things in response

                    Now that on the other hand would be irritating.

                    Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                    1 Reply Last reply
                    0
                    • K Kevin Marois

                      I have a MenuButton on my Main Window toolbar:

                      Here's the DropDown button code:

                      public class DropDownButton : ToggleButton
                      {
                      public DropDownButton()
                      {
                      // Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property
                      Binding binding = new Binding("Menu.IsOpen");
                      binding.Source = this;
                      this.SetBinding(IsCheckedProperty, binding);
                      DataContextChanged += (sender, args) =>
                      {
                      if (Menu != null)
                      Menu.DataContext = DataContext;
                      };
                      }

                      public ContextMenu Menu
                      {
                          get { return (ContextMenu)GetValue(MenuProperty); }
                          set { SetValue(MenuProperty, value); }
                      }
                      public static readonly DependencyProperty MenuProperty = DependencyProperty.Register("Menu", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null, OnMenuChanged));
                      
                      private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
                      {
                          var dropDownButton = (DropDownButton)d;
                          var contextMenu = (ContextMenu)e.NewValue;
                          contextMenu.DataContext = dropDownButton.DataContext;
                      }
                      
                      protected override void OnClick()
                      {
                      
                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      Kevin Marois wrote:

                      The problem is that the CanExecute doesn't fire until I actually click on a menu bar.

                      As far as I can see, that would be the expected behaviour. The system doesn't need to know whether the menu item is enabled or not until the menu is shown, so I wouldn't expect it to call CanExecute for the menu's command until you click on the button to show the menu.


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      1 Reply Last reply
                      0
                      • K Kevin Marois

                        I have a MenuButton on my Main Window toolbar:

                        Here's the DropDown button code:

                        public class DropDownButton : ToggleButton
                        {
                        public DropDownButton()
                        {
                        // Bind the ToogleButton.IsChecked property to the drop-down's IsOpen property
                        Binding binding = new Binding("Menu.IsOpen");
                        binding.Source = this;
                        this.SetBinding(IsCheckedProperty, binding);
                        DataContextChanged += (sender, args) =>
                        {
                        if (Menu != null)
                        Menu.DataContext = DataContext;
                        };
                        }

                        public ContextMenu Menu
                        {
                            get { return (ContextMenu)GetValue(MenuProperty); }
                            set { SetValue(MenuProperty, value); }
                        }
                        public static readonly DependencyProperty MenuProperty = DependencyProperty.Register("Menu", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null, OnMenuChanged));
                        
                        private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
                        {
                            var dropDownButton = (DropDownButton)d;
                            var contextMenu = (ContextMenu)e.NewValue;
                            contextMenu.DataContext = dropDownButton.DataContext;
                        }
                        
                        protected override void OnClick()
                        {
                        
                        A Offline
                        A Offline
                        amcic1990
                        wrote on last edited by
                        #11

                        the CanExecute doesn't fire until I actually click on a menu bar.

                        [url=https://www.pornjk.com][color=#000000]pornjk[/color][/url] [url=https://www.porn800.me][color=#000000]porn800[/color][/url] [url=https://www.redtube.social][color=#000000]redtube[/color][/url]

                        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