How can I initiate routed commands implemented inside UserControl that are nested inside ContentControl?
I basically have an appearance (derived from UserControl) that contains:
1) Button that should run the MyCommand command: Here, the CommandTarget is clearly mistaken, since it should be the view that is placed inside the ContentControl, and not the content control itself, because the CommandBinding is added to the CommandBindings InnerView collection.
<Button Command="{x:Static Commands:MyCommands.MyCommand}" CommandTarget="{Binding ElementName=ViewHost}">
Trigger Command
</Button>
2) A ContentControl. The Content property is bound to the ViewModel to be used by the internal view:
<ContentControl x:Name="ViewHost" Content="{Binding InnerViewModel}" />
3) DataTemplate, which determines the type of internal view:
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type ViewModels:InnerViewModel}">
<Views:InnerView />
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
InnerView ( UserControl) CommandBinding Loaded event:
public partial class InnerView : UserControl
{
private void InnerViewLoaded(object sender, System.Windows.RoutedEventArgs e)
{
view.CommandBindings.Add(new CommandBinding(MyCommands.MyCommand, this.ExecuteMyCommand, this.CanExecuteMyCommand));
}
}
, , , :
internal class MyCommands
{
static MyCommands()
{
MyCommand = new RoutedCommand("MyCommand", typeof(MyCommands));
}
public static RoutedCommand MyCommand { get; private set; }
}
? , , CommandTarget . CommandTarget , ContentControl?
InnerView OuterView Button CommandTarget InnerView, :
<Views:InnerView x:Name="InnerViewInstance" />
<Button Command="{x:Static Commands:MyCommands.MyCommand}" CommandTarget="{Binding ElementName=InnerViewInstance}">
Trigger Command
</Button>