Reputation: 132
We would like define some button be visible in every page for a set of users. We defined button as control template for view and called out it in the pages that require them to be displayed.
<Button ImageSource="home_footer.png" BackgroundColor="Transparent" HorizontalOptions="FillAndExpand"
Command="{TemplateBinding BindingContext.HomeCommand}"/>
Here command is selected according to the binding context, i.e corresponding view model. Command need to written multiple times. I thought of including the command in base view model so that it needs to written only once. Is there any option for implementing this option. Thanks in advance.
Upvotes: 2
Views: 127
Reputation: 16459
In your BaseViewModel add a command
public ICommand HomeCommand { get; set; }
In your base ContentPage
add a bindable property that supplies you this command:
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
public object CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
public static readonly BindableProperty CommandProperty = BindableProperty.Create(
nameof(Command),
typeof(ICommand),
typeof(ClassName),
null
);
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(
nameof(CommandParameter),
typeof(object),
typeof(ClassName),
null
);
Supply a command to this property from the ContentPage
<base:CustomContentPage ....
Command="{Binding HomeCommand}">
Set it to your Button something like:
You can give it a better name to explain the behaviour,
Goodluck!
Feel free to get back if you have queries!
Upvotes: 2