Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Custom Control Binding to Command in Parent View Model?

$
0
0

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:

  1. MyPage.xaml ( BindingContent is MyPageViewModel )
  2. within the XAML page I have created a child view -> local:MyContentView
  3. 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


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>