Halloween_Udo
Halloween_Udo

Reputation: 69

Routed Command Implementation in WPF

I have simple ICommand-Bindings working, however I have Buttons inside an ItemsControl and wanted to get the sender information, like with the normal Routed-Events (object sender, e RoutedEventArgs) and this seems not to be possible with the normal ICommands, right?

I am a little bit lost here.

I currently use the Prism 6 DelegateCommand-Class to get things working. It looks like this:

private ICommand _selectCommand;
public ICommand SelectCommand
{
    get
    {
        return _selectCommand ?? (_selectCommand = new DelegateCommand<object>(SelectImage));
    }
}

private void SelectImage(object image)
{
    var img = (BitmapImage)image;
    var index = Scans.IndexOf(img);
    this.CurrentIndex = index + 1;
    ImageToDisplay = img;
} 

How I can I get the RoutedCommand to work?

Upvotes: -1

Views: 129

Answers (1)

mm8
mm8

Reputation: 169270

A view model is not supposed to be accessing or even know about any view element.

You should bind a target property of the control in the view to a source property of the view model that you can simply set in your SelectImage method when your command gets executed.

Upvotes: 1

Related Questions