Call RelayCommand in WPF-Control from WPF-Windows (XAML)

236 views Asked by At

I have a WPF UserControl with a ViewModel as DataContext. This ViewModel also contains RelayCommands. From the control I can call these commands (e.g. with a button) <Button Command="{Binding SaveCommand}">Save</Button>. Now I want to place the control in a window and call the commands from there <Button Command="{Binding SaveCommand}" CommandTarget="{Binding ElementName=myControl}">Save</Button>. But this does not work. How can I call a command in a child control in XAML?

Thanks a lot Markus

1

There are 1 answers

0
Lana On BEST ANSWER

If your main window has different DataContext and you can't reach this command through it you can write

<Button Command="{Binding DataContext.SaveCommand, ElementName=myControl}">Save</Button>