Alex Rivera | Logout

WPF Built-in Commands

Asked 2009-10-15T20:20:09.497
34

I'm looking for a complete list of built-in WPF Commands.

The best list I've found so far is here, but it does not list all commands.

Some nice-to-have details would be:

  • Controls/components with support to the commands (for example, TextBox supports edit commands such as Paste, Copy, Cut, Redo, and Undo);

  • Default Key Gestures and UI Text (can be extracted from MSDN Library).

Edit
Report

1 Answer

90

It is easy to display a complete list of all commands in all loaded assemblies:

  public string[] GetAllCommands()
  {
    return (
      from assembly in AppDomain.CurrentDomain.GetAssemblies()
      from type in assembly.GetTypes()
      from prop in type.GetProperties()
      where
        typeof(ICommand).IsAssignableFrom(prop.PropertyType) &&
        prop.GetAccessors()[0].IsStatic
      orderby type.Name, prop.Name
      select type.Name + "." + prop.Name
    ).ToArray();
  }

With PresentationFramework loaded I get the list at the bottom of this answer, which you will see is absolutely complete.

If you also want to see the command type (eg RoutedUIComand) and gestures, you can add this to the LINQ:

      let commandType = prop.PropertyType

      let gestures =
        typeof(UIElement).IsAssignableFrom(commandType) ?
          ((UIElement)prop.GetValue(null)).InputGestures :
        null

Then your select might be something like this:

      select type.Name + "." + prop.Name + " " + commandType.Name + " " + gestures

Programmatically finding out which controls do something with a given command is also possible. Basically something like this ought to work (not tried, but this will give you the idea):

var allCommands = (
  from assembly in AppDomain.CurrentDomain.GetAssemblies()
  from type in assembly.GetTypes()
  from prop in type.GetProperties()
  where
    typeof(ICommand).IsAssignableFrom(prop.PropertyType) &&
    prop.GetAccessors()[0].IsStatic
  orderby type.Name, prop.Name
  select new
  {
    typeName = type.Name,
    propName = prop.Name,
    displayAs = type.Name + "." + prop.Name,
  }
).ToArray();

var classesReferencingCommand = (
  from assembly in AppDomain.CurrentDomain.GetAssemblies()
  from type in assembly.GetTypes()
  from method in type.GetMethods()
  let methodBodyString = ConvertILToString(method.
answered 2009-11-06T22:56:57.030

Your Answer