You can use a multi data trigger in your button to enable or disable it depending on the status of your picker selection properties.
Because you are binding to strings, you will need add a converter to test whether they are null or empty, for example:
namespace Osma.Mobile.App.Converters
{
public class StringNullOrEmptyValueBoolConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string str && !string.IsNullOrEmpty(str))
{
// String is not null or empty
return false;
}
// String is null or empty
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Add the required properties to your ViewModel:
public class ProviderViewModel : ABaseViewModel
{
public ProviderViewModel(
IUserDialogs userDialogs,
INavigationService navigationService
) : base(
nameof(ProviderViewModel),
userDialogs,
navigationService)
{
}
public override async Task InitializeAsync(object navigationData)
{
await base.InitializeAsync(navigationData);
}
private string _selectedProvider;
public string SelectedProvider {
get { return _selectedProvider; }
set
{
this.RaiseAndSetIfChanged(ref _selectedProvider, value) }
}
}
private string _selectedCountry;
public string SelectedCountry {
get { return _selectedCountry; }
set
{
this.RaiseAndSetIfChanged(ref _selectedCountry, value) }
}
}
public async Task RegistrationPage(RegisterViewModel registration) => await NavigationService.NavigateToAsync(registration, null, NavigationType.Modal);
public ICommand RegistrationPageCommand => new Command<RegisterViewModel>(async (registration) =>
{
await RegistrationPage(registration);
}, (x) => false);
}
Add the converter namespace and resource to your page and implement the multitrigger and bindings:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:Osma.Mobile.App.Converters"
x:Class="Osma.Mobile.App.Views.ProviderPage"
NavigationPage.HasNavigationBar="False"
BackgroundColor="#004B86">
<ContentPage.Resources>
<ResourceDictionary>
<converters:StringNullOrEmptyValueBoolConverter x:Key="stringNullOrEmptyValueBoolConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout
Spacing="30">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="70" />
<RowDefinition Height="70" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Picker
x:Name="picker_country"
Title="Select a Country"
TextColor="Silver"
TitleColor="Silver"
HorizontalOptions="Start"
WidthRequest = "200"
VerticalOptions="Start"
Margin="130,0,0,0"
SelectedItem="{Binding SelectedCountry}">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Sweden</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
</Grid>
<Grid Grid.Row="1">
<Picker
x:Name="picker_provider"
Title="Select a Provider"
TextColor="Silver"
TitleColor="Silver"
HorizontalOptions="Start"
WidthRequest = "200"
VerticalOptions="Start"
Margin="130,0,0,0"
SelectedItem="{Binding SelectedProvider}">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Provider 1</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
</Grid>
</Grid>
<Button
BackgroundColor="#2194EF"
TextColor="White"
Text="Next"
HeightRequest="60"
MinimumHeightRequest="60"
IsEnabled="False"
Command="{Binding RegistrationPageCommand}">
<Button.Triggers>
<MultiTrigger TargetType="Button">
<MultiTrigger.Conditions>
<BindingCondition Binding="{Binding SelectedCountry,
Converter={StaticResource stringNullOrEmptyValueBoolConverter }}" Value="false" />
<BindingCondition Binding="{Binding SelectedProvider,
Converter={StaticResource stringNullOrEmptyValueBoolConverter }}" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="IsEnabled" Value="True" />
</MultiTrigger>
</Button.Triggers>
</Button>
</StackLayout>
</ContentPage.Content>
</ContentPage>
As you can see in the code the multrigger conditions are that both properties are not null or empty and when this is the case the button will be enabled.