tags:

views:

31

answers:

1

I am having trouble binding a Command that is generated up the UI tree to a control. The following example illustrates my point, the CommandBinding in Grid does not act on the InputBindings in Window. Maybe I do not understand the point of commands, but I would like to have a nice solution for child controls to act on user input on the Window (any control on the Window).

<Window x:Class="SilverFit.Menu.Wpf.WpfWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Window.InputBindings>
        <KeyBinding Command="Close" Key="Escape"/>
        <MouseBinding Command="Close" MouseAction="RightClick" />
    </Window.InputBindings>
    <Grid Name="grid">
        <Grid.CommandBindings>
            <CommandBinding Command="Close" Executed="Close"/>
        </Grid.CommandBindings>
    </Grid>
</Window>
A: 

What object is acting as your command target? Whatever it is, it will need to be a child of the grid.

Tim Lovell-Smith
The command target is the Window in this example since the binding executes the Close method on the Window class.
Wouter
Then the answer is that command bindings on the Grid can only see commands which are targeted at things *inside* the grid, or that binding a routed command in a child doesn't really make sense - because of the routed event model.
Tim Lovell-Smith