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. Button Bound To Command Won't Disable

Button Bound To Command Won't Disable

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

    Working on a WPF MVVM app... I have this button:

    It's bound to RunCommand:

    private ICommand _RunCommand;
    public ICommand RunCommand
    {
    get
    {
    if (_RunCommand == null)
    {
    _RunCommand = new RelayCommand(runTest, runTestCanExecute);
    }
    return _RunCommand;
    }
    }

    Here's the methods:

    private void runTest()
    {
    isTesting = true;

    var device = (from d in tempDevices
                    where d.DeviceId == SelectedDevice.DeviceId
                    select d).FirstOrDefault();
    
    diag.ExecuteTest(device, SelectedTest);
    

    }
    private bool runTestCanExecute()
    {
    return !isTesting;
    }

    isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?

    If it's not broken, fix it until it is

    S M R P 4 Replies Last reply
    0
    • K Kevin Marois

      Working on a WPF MVVM app... I have this button:

      It's bound to RunCommand:

      private ICommand _RunCommand;
      public ICommand RunCommand
      {
      get
      {
      if (_RunCommand == null)
      {
      _RunCommand = new RelayCommand(runTest, runTestCanExecute);
      }
      return _RunCommand;
      }
      }

      Here's the methods:

      private void runTest()
      {
      isTesting = true;

      var device = (from d in tempDevices
                      where d.DeviceId == SelectedDevice.DeviceId
                      select d).FirstOrDefault();
      
      diag.ExecuteTest(device, SelectedTest);
      

      }
      private bool runTestCanExecute()
      {
      return !isTesting;
      }

      isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?

      If it's not broken, fix it until it is

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      Are you sure its not disabled? You are overriding the content, so it may appear to be enabled, but really be not clickable because you aren't handling the disabled state.

      K 1 Reply Last reply
      0
      • K Kevin Marois

        Working on a WPF MVVM app... I have this button:

        It's bound to RunCommand:

        private ICommand _RunCommand;
        public ICommand RunCommand
        {
        get
        {
        if (_RunCommand == null)
        {
        _RunCommand = new RelayCommand(runTest, runTestCanExecute);
        }
        return _RunCommand;
        }
        }

        Here's the methods:

        private void runTest()
        {
        isTesting = true;

        var device = (from d in tempDevices
                        where d.DeviceId == SelectedDevice.DeviceId
                        select d).FirstOrDefault();
        
        diag.ExecuteTest(device, SelectedTest);
        

        }
        private bool runTestCanExecute()
        {
        return !isTesting;
        }

        isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?

        If it's not broken, fix it until it is

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

        Try this, change you button to use a Click="" event instead of a Command.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • S SledgeHammer01

          Are you sure its not disabled? You are overriding the content, so it may appear to be enabled, but really be not clickable because you aren't handling the disabled state.

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

          Ya, it's still responds to clicks.

          If it's not broken, fix it until it is

          S 1 Reply Last reply
          0
          • K Kevin Marois

            Ya, it's still responds to clicks.

            If it's not broken, fix it until it is

            S Offline
            S Offline
            SledgeHammer01
            wrote on last edited by
            #5

            Copy & pasted your code into a test project and it disables just fine. Make sure your DataContext is set properly or it won't know where the command is. I.e. put a break point on your CanExecute handler and make sure its actually getting called.

            K 1 Reply Last reply
            0
            • S SledgeHammer01

              Copy & pasted your code into a test project and it disables just fine. Make sure your DataContext is set properly or it won't know where the command is. I.e. put a break point on your CanExecute handler and make sure its actually getting called.

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

              Ok, I see what's going on. isTesting is somehow reverting to false by the time the CanExecute is called. Stay tuned.....

              If it's not broken, fix it until it is

              1 Reply Last reply
              0
              • K Kevin Marois

                Working on a WPF MVVM app... I have this button:

                It's bound to RunCommand:

                private ICommand _RunCommand;
                public ICommand RunCommand
                {
                get
                {
                if (_RunCommand == null)
                {
                _RunCommand = new RelayCommand(runTest, runTestCanExecute);
                }
                return _RunCommand;
                }
                }

                Here's the methods:

                private void runTest()
                {
                isTesting = true;

                var device = (from d in tempDevices
                                where d.DeviceId == SelectedDevice.DeviceId
                                select d).FirstOrDefault();
                
                diag.ExecuteTest(device, SelectedTest);
                

                }
                private bool runTestCanExecute()
                {
                return !isTesting;
                }

                isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?

                If it's not broken, fix it until it is

                R Offline
                R Offline
                RobCroll
                wrote on last edited by
                #7

                I haven't got VS at hand at the moment but couldn't you just do something along the lines of:

                "You get that on the big jobs."

                1 Reply Last reply
                0
                • K Kevin Marois

                  Working on a WPF MVVM app... I have this button:

                  It's bound to RunCommand:

                  private ICommand _RunCommand;
                  public ICommand RunCommand
                  {
                  get
                  {
                  if (_RunCommand == null)
                  {
                  _RunCommand = new RelayCommand(runTest, runTestCanExecute);
                  }
                  return _RunCommand;
                  }
                  }

                  Here's the methods:

                  private void runTest()
                  {
                  isTesting = true;

                  var device = (from d in tempDevices
                                  where d.DeviceId == SelectedDevice.DeviceId
                                  select d).FirstOrDefault();
                  
                  diag.ExecuteTest(device, SelectedTest);
                  

                  }
                  private bool runTestCanExecute()
                  {
                  return !isTesting;
                  }

                  isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?

                  If it's not broken, fix it until it is

                  P Offline
                  P Offline
                  PaulLinton
                  wrote on last edited by
                  #8

                  Whenever you change isTesting you will need to call

                  RunCommand.OnCanExecuteChanged()

                  (If it isn't, you might want to make isTesting a property and call OnExecuteChanged from the Setter)

                  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