I have two buttons in one View, is it possible to bind one button to one command in first ViewModel and another button bind to second command in second ViewModel? Just to add both ViewModels are initialized.
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.FirstCommand}" CommandParameter="{Binding NameE}">
<Label Content="{Binding NameE}"/>
</Button>
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.SecondCommand}" CommandParameter="{Binding NameD}">
<Label Content="{Binding NameD}"/>
</Button>
And my two ViewModels:
public class FirstViewModel : INotifyPropertyChanged
{
public MyICommand<string> FirstCommand { get; private set; }
public HomeViewModel()
{
FirstCommand = new MyICommand<string>(myFirstCommand);
}
public void myFirstCommand(string par)
{
Console.Beep();
}
}
public class SecondViewModel : INotifyPropertyChanged
{
public MyICommand<string> SecondCommand { get; private set; }
public HomeViewModel()
{
SecondCommand = new MyICommand<string>(mySecondCommand);
}
public void mySecondCommand(string par)
{
Console.Beep();
}
}
EDIT: In a view where are buttons i have <ContentPresenter Content="{Binding Content}"/> and that Content have View with a ViewModel i wanna reach with buttons
<ContentPresenter Content="{Binding Content}"/>and that Content have View with a ViewModel i wanna reach with buttons. - Filip Filipovic