Custom view with binding Property does not bind properly to SAP Xamarin.Forms

I have a flag that should trigger the IsEnabled button event. But somehow the team that was supposed to do this will never be properly attached and therefore executed.

This is the bindable property in CheckBox.xaml.cs (control):

public static readonly BindableProperty CheckBoxCommandProperty = BindableProperty.Create<CheckBox, ICommand>( checkbox => checkbox.CheckBoxCommand, null, propertyChanged: (bindable, oldValue, newValue) => { CheckBox checkbox = (CheckBox)bindable; EventHandler<bool> eventHandler = checkbox.CheckedChanged; if (eventHandler != null) { eventHandler(checkbox, checkbox.IsChecked); } }); public event EventHandler<bool> CheckedChanged; public ICommand CheckBoxCommand { get { return (ICommand)GetValue(CheckBoxCommandProperty); } set { SetValue(CheckBoxCommandProperty, value); } } 

And here is what I see in the ViewModel:

  public ICommand Next_OnClick { get; set; } public ICommand OnCheckBoxTapChanged { get; set; } public TermsAndConditionsViewModel() { Next_OnClick = new Command(NextClicked); OnCheckBoxTapChanged = new Command(CheckBoxTapped); } private void CheckBoxTapped() { if (IsCheckedChanged) { Next_IsEnabled = true; } else { Next_IsEnabled = false; } } 

The CheckBoxTapped method never starts, so the button property that I want to set never changes.

Thanks in advance!

+4
source share
2 answers

Solved it using Xamarin.Forms.Behavior. It allows you to manage multiple relationships.

For example>

 <Entry Placeholder="Enter password" Text= "{Binding TxtFirstPasswordInput}" IsPassword="true"> <b:Interaction.Behaviors> <b:BehaviorCollection> <b:EventToCommand EventName="Unfocused" Command="{Binding entry_Finished}"/> <b:EventToCommand EventName="Focused" Command="{Binding entry_Focused}"/> </b:BehaviorCollection> </b:Interaction.Behaviors> </Entry> 

And on ViewModel>

 public PasswordInputViewModel() { entry_Finished = new Command(validateAndContinue);//unfocused entry_Focused = new Command(entryFocused); //focused } 
0
source

What you can do is split the solution to this problem into several levels:

First create a bindable property for this check box and call it Validated. I wrote a snippet for it, but didn’t actually compile it, so you can change it if it doesn’t work.

  public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create<CheckBox, bool> (w => w.IsChecked, false); public bool IsChecked{ get { return GetValue (FooProperty); } set { SetValue (FooProperty, value); } } 

Secondly, in the view model, the bool property has a change notification

 private bool _isChecked; public bool IsChecked { get { return _isChecked; } set { _isChecked = value; RaisePropertyChanged("IsChecked"); } } 

Thirdly, bind your bindable property to the "isChecked" checkbox with one in the view model in your xaml:

 <StackLayout> <Checkbox IsChecked = "{Binding IsChecked}"/> </StackLayout> 

Fourth, buttons in Xaml with MVVM are associated with commands. and in these commands for them there is a bool property that represents "CanExecute", which basically turns the button on or off. therefore, what you need to do in Xaml is to bind the button command to the command in Model View (allows you to call ClickCommand). And ClickCommand CanExecute is actually a method that returns the value "IsChecked". This will force us to change the installer of the "IsChecked" property, so every time it changes, it must notify the command to check the CanExecute property.

So the final code will be something like

 public TermsAndConditionsViewModel() { NextCommand = new Command(ExecuteNextCommand,CanExecuteNextCommand); OnCheckBoxTapChanged = new Command(CheckBoxTapped); } private bool _isChecked; public bool IsChecked { get { return _isChecked;} set { _isChecked = value; NextCommand.ChangeCanExecute(); //this will actually notify the command to enable/disable RaisePropertyChanged("IsChecked"); } } public Command NextCommand {get;set;} // this type is available in Xamarin.Forms library private bool CanExecuteNextCommand() { return IsChecked; } private void ExecuteNextCommand() { // your execution when the button is pressed } 

And xaml will look like

 <StackLayout> <Checkbox IsChecked = "{Binding IsChecked}"/> <Button Text= "Next" Command = "{Binding NextCommand}"/> </StackLayout> 
+2
source

All Articles