views:

273

answers:

1

The constructor of the RoutedCommand has "owner type" as a last argument. What is its significance? When it is used?

MSDN documentation gives completely no clue about why it's needed and whether I could use one type for all commands

Quote from MSDN

ownerType
     Type: System.Type The type
     which is registering the command.

There is one more thing. What type should I use when creating new routed commands dynamically from array of names. It looks like that any type works, so I'm using UIElement, but if there is a more suited type for this I would like to know.

+2  A: 

The source for RoutedCommand shows that the type becomes the OwnerType property. This property is queried ultimately by the following private method when getting InputGestures. So it looks as though this type is being used to lookup a (hard-coded) set of Commands based on the type that created the RoutedCommand.

private InputGestureCollection GetInputGestures()
{
    if (this.OwnerType == typeof(ApplicationCommands))
{
    return ApplicationCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(NavigationCommands))
{
    return NavigationCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(MediaCommands))
{
    return MediaCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(ComponentCommands))
{
    return ComponentCommands.LoadDefaultGestureFromResource(this._commandId);
}
return new InputGestureCollection();
}
dpp
Anybody can explain why?
Sergej Andrejev
Probably just "because." As good a reason for API design as any other.</sarcasm> *blech*
David Schmitt