void ExecuteCommand() { if (CheckChangedCommand?.CanExecute(CommandParameter ?? this) ?? false) { CheckChangedCommand?.Execute(CommandParameter ?? this); } }
/// <summary> /// Initializes the default view /// </summary> public void InitializeDefaultView() { // Return if view is already initialized if (ViewInitialized) { return; } // The grid who is in the bottom root Grid rootGrid = new Grid() { Children = { new Label() { FontSize = Device.GetNamedSize(FontSize, typeof(Label)), TextColor = this.TextColor, FontFamily = this.FontRegularFamily, VerticalOptions = LayoutOptions.Center, Text = UncheckedFont } } }; // Add tap to set check or unchecked this.GestureRecognizers.Add(new TapGestureRecognizer() { Command = new Command(() => { IsChecked = !IsChecked; if (IsChecked) { // If is checked, we add a check font rootGrid.Children.Add(new Label() { FontSize = Device.GetNamedSize(FontSize, typeof(Label)), TextColor = this.TextColor, FontFamily = this.FontFamily, VerticalOptions = LayoutOptions.Center, Text = CheckedFont }); } else if (rootGrid.Children.Count > 1) { // If unchecked, we remove that check font var toRemove = rootGrid.Children.LastOrDefault(); rootGrid.Children.Remove(toRemove); } // If there is a binding on command we invoke it here CheckChangedCommand?.Execute(null); }) }); // Add root this.Children.Add(rootGrid); // Add a title label if there is one if (!string.IsNullOrEmpty(Title)) { var header = new Label() { Text = Title, TextColor = TextColor, VerticalOptions = LayoutOptions.Center, FontSize = Device.GetNamedSize(FontSize, typeof(Label)) }; if (FlowRightToLeft) { this.Children.Insert(0, header); } else { this.Children.Add(header); } } ViewInitialized = true; }
/// <summary> /// Default Constructor /// </summary> public CheckBox() { this.Orientation = StackOrientation.Horizontal; this.Margin = new Thickness(10, 0); this.Padding = new Thickness(10); this.Spacing = 10; this.Children.Add(new Grid { Children = { boxBackground, boxSelected } }); this.Children.Add(lblOption); this.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => { if (IsDisabled) { return; } IsChecked = !IsChecked; CheckChanged?.Invoke(this, new EventArgs()); CheckChangedCommand?.Execute(this.IsChecked); }), }); }
/// <summary> /// Default Constructor /// </summary> public CheckBox() { this.Orientation = StackOrientation.Horizontal; this.Padding = new Thickness(0, 10); this.Spacing = 10; boxBackground.Content = boxSelected; this.Children.Add(boxBackground); //this.Children.Add(new Grid { Children = { boxBackground, boxSelected }, MinimumWidthRequest = 35 }); this.Children.Add(lblOption); this.GestureRecognizers.Add(new TapGestureRecognizer { Command = new Command(() => { if (IsDisabled) { return; } IsChecked = !IsChecked; CheckChanged?.Invoke(this, new EventArgs()); CheckChangedCommand?.Execute(this.IsChecked); ValidationChanged?.Invoke(this, new EventArgs()); }), }); }