Consider the code snippet below for a second:
<StackPanel>
<local:ChildElementWithCommand x:Name="ChildElementWithCommand">
<Button Content="Test"> // Bind this button to a command from ChildElementWithCommand
</StackPanel>
How to bind this Button to a command from the usercontrol named ChildElementWithCommand (I am pretty sure that this is not a particularly clean architecture)
ChildElementWithCommand.xaml.cs
namespace Test
{
public partial class ChildElementWithCommand : UserControl
{
public ViewModel vm;
public ICommand MyCommand;
public ChildElementWithCommand()
{
InitializeComponent();
vm = new vm();
MyCommand = vm.MyCommand; // I would like to use this command in Button above
}
}
}
CodePudding user response:
This way
<StackPanel>
<local:ChildElementWithCommand x:Name="ChildElementWithCommand">
<Button Content="Test" Command="{Binding ElementName=ChildElementWithCommand, Path=SomeCommand}">
</StackPanel>
'Path=
' is optional.