# Generic routed event handler to command behavior

## Scenario

Imagine that you need to handle from the ViewModel the `SelectedIndexChanged` of a WPF `TreeView`, currently the only way (without using any particular framework) is to build your own behavior to achieve that, or bind, via a style the `IsSelected` property of the node to a property of the view model, but in this second case the side effect is that to find the selected item you need to visit the whole tree.

## Radical “Handle”

```markup
<TreeView Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                     ItemsSource="{Binding Path=MyListOfElements}">
    <i:Interaction.Behaviors>
        <behaviors:Handle RoutedEvent="TreeView.SelectedItemChanged"
                          WithCommand="{Binding Path=MyAmazingCommand}"
                          PassingIn="$args.NewValue" />
    </i:Interaction.Behaviors>
    <TreeView.ItemTemplate>
        <!-- omitted -->
    </TreeView.ItemTemplate>
</TreeView>
```

As you can imagine the value of the `NewValue` property of the event arguments is passed as the command parameter to the command, we currently support as placeholder:

* `$args`: the routed event arguments;
* `$this`: the WPF element the behavior is attached to;
* `$source`: the source of the routed event;
* `$originalSource`: the original source of the routed event;

### Notes:

* the event identified by the `RoutedEvent` property must be a valid WPF `RoutedEvent`.
* the bound command identified by the `WithCommand` property must be a valid `ICommand`, `AutoCommandBinding` is not supported.

The behavior is defined in the `http://schemas.radicalframework.com/windows/behaviors` xml namespace.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.radicalframework.com/release-2/behaviors/routed-event-to-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
