I have a custom control within a ContentView and I would like the ability to Bind to a Command in the Parent View Model.
Is this possible, does MyContentView inherit from the Parent Page?
My design is as follows:
- MyPage.xaml ( BindingContent is MyPageViewModel )
- within the XAML page I have created a child view -> local:MyContentView
- and within the child view is a custom control -> local:CustomButtom
Within CustomButton.cs I have the following bindable properties
public class ToggleButton : ContentView
{
public static readonly BindableProperty CommandProperty =
BindableProperty.Create("Command", typeof(ICommand), typeof(ToggleButton), null);
public static readonly BindableProperty CommandParameterProperty =
BindableProperty.Create("CommandParameter", typeof(object), typeof(ToggleButton), null);
public ICommand Command
{
get { return (ICommand) GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public object CommandParameter
{
get { return GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
and to execute the Command I use this
if (Command != null)
{
Command.Execute(CommandParameter);
}
However when I add debug break point to the above 'if' statement
Command is always null and does not Bind to the Command in the Parent ViewModel