I suggest you ...

Something like ICommand but with state (e.g. for a ToggleButton)

Currently you can hook up an ICommand to a button and have it invoke the command when the button is pushed, and the IsEnabled state of the button will change when the CanExecute state of the ICommand changes.

What you can't do is automatically reflect any other state that relates to the command on the UI.

The most obvious example I can think of is a "Bold" ToggleButton on the toolbar of a text editor. Clicking the button should toggle whether the slected text is bold or not, but the toggle state of the button should reflect whether the *currently selected* text is bold right now, and should be able to be updated from wherever the ICommand comes from.

Also consider global on/off options which may be accessible through a ToggleButton on a toolbar and a (checkable) MenuItem. The state of both bits of UI should always reflect the state of the option.

5 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    swythanswythan shared this idea  ·   ·  Admin →

    2 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • mike.strobelmike.strobel commented  · 

        This can be done without extending the ICommand API. You can do it via a command parameter. For example, a CheckableCommandParameter class, with an "IsChecked" bool or Nullable<bool> property would do the trick. A ToggleButton's "IsChecked" property could bind to its CommandParameter's "IsChecked" property, and the parameter's property could updated by the command's Execute and CanExecute handlers. Extending ICommand is not the best approach, IMO, as additional state breaks the pattern.

      • Josh EinsteinJosh Einstein commented  · 

        This is a pretty good idea. Back in the dark days of Windows Forms when I implemented my own commanding framework, I had a CommandState flags enumeration that contained states such as None, Enabled, Visible, Toggled. The idea being that commands are not simply enabled/disabled. They may be wholly unapplicable (so don't show it at all) or they may be currently "in effect" (so show it checked/down). I vote for an extension of ICommand.

      Feedback and Knowledge Base