ICommand binding from a TapGestureRecognizer not working
sonal khatri
51
Reputation points
Hi,
I am not able to bind ICommand property to the label
Here is my code:
using Syncfusion.ListView.XForms;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Xamarin.Forms;
using SelectionMode = Syncfusion.ListView.XForms.SelectionMode;
namespace GestureRecognizer
{
public class TapView : ContentPage
{
#region Private constants
private const int RowHeight = 49;
#endregion
#region Binding
//public static readonly BindableProperty ProductProperty = BindableProperty.Create(
// nameof(ProductList), typeof(ObservableCollection<Product>), typeof(ObservableCollection<Product>), null,
// BindingMode.OneWay, null, (bindable, oldValue, newValue) =>
// {
// ((TapView)bindable).ProductList = (ObservableCollection<Product>)newValue;
// });
#endregion
SfListView listView;
#region Public properties
//public ObservableCollection<Product> ProductList
//{
// get => (ObservableCollection<Product>)ItemsSource;
// private set
// {
// ItemsSource = value;
// }
//}
//private List<TapLookupColumnDefinition> _columnDefinitions = new List<TapLookupColumnDefinition>();
//public new List<TapLookupColumnDefinition> ColumnDefinitions
//{
// get => _columnDefinitions;
// set
// {
// _columnDefinitions = value;
// OnColumnDefinitionsChanged();
// }
//}
#endregion
#region Constructors
public TapView()
{
//InitializeComponent();
TapModel viewModel = new TapModel();
TapViewModel tapViewModel = new TapViewModel();
listView = new SfListView();
List<TapLookupColumnDefinition> ColumnDefinitions = new List<TapLookupColumnDefinition>();
listView.ItemsSource = viewModel.Products;
listView.ItemTemplate = new DataTemplate(() =>
{
Grid bodyLayout = new Grid
{
ColumnSpacing = 0,
RowSpacing = 0,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
bodyLayout.RowDefinitions.Add(new RowDefinition { Height = RowHeight });
bodyLayout.RowDefinitions.Add(new RowDefinition { Height = 1 });
Grid checkLayout = new Grid
{
ColumnSpacing = 0,
RowSpacing = 0,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
for (int i = 0; i < 4; i++)
{
AddCell(checkLayout.Children, i, nameof(Product.Number));
}
Grid.SetRow(checkLayout, 0);
bodyLayout.Children.Add(checkLayout);
BoxView separatorBoxView = new BoxView
{
BackgroundColor = Color.Gray,
Margin = 0
};
Grid.SetRow(separatorBoxView, 1);
bodyLayout.Children.Add(separatorBoxView);
// Single tap
TapGestureRecognizer gestureRecognizer = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, nameof(TapViewModel.TapCommand));
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
bodyLayout.GestureRecognizers.Add(gestureRecognizer);
return bodyLayout;
});
Content = listView;
}
private static Label CreateLabel(string automationId = null)
{
Label label = new Label
{
HorizontalTextAlignment = TextAlignment.Center,
FontSize = 15,
LineBreakMode = LineBreakMode.WordWrap,
Margin = new Thickness(10, 0, 10, 0),
VerticalTextAlignment = TextAlignment.Center,
AutomationId = automationId,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
TapGestureRecognizer gestureRecognizer = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, nameof(TapViewModel.TapCommand));
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
label.GestureRecognizers.Add(gestureRecognizer);
return label;
}
private static BoxView CreateBoxView(int column, string automationId = null)
{
BoxView boxView = new BoxView
{
BackgroundColor = column % 2 == 0 ? Color.White : Color.Gray,
AutomationId = automationId
};
return boxView;
}
private static Tuple<BoxView, Label> AddCell(ICollection<View> gridChildren, int column,
string textBindingProperty)
{
BoxView cellBoxView = CreateBoxView(column);
Grid.SetColumn(cellBoxView, column);
gridChildren.Add(cellBoxView);
Label cellLabel = CreateLabel(textBindingProperty);
cellLabel.SetBinding(Label.TextProperty, textBindingProperty);
//cellLabel.HorizontalOptions = LayoutOptions.FillAndExpand;
//cellLabel.VerticalOptions = LayoutOptions.FillAndExpand;
//// Single tap
//TapGestureRecognizer gestureRecognizer = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
//gestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, nameof(CheckWrapperModel.ClickCommand));
//gestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
//cellLabel.GestureRecognizers.Add(gestureRecognizer);
//// Double tap
//gestureRecognizer = new TapGestureRecognizer { NumberOfTapsRequired = 2 };
//gestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, nameof(CheckWrapperModel.DoubleTapCommand));
//gestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
//cellLabel.GestureRecognizers.Add(gestureRecognizer);
Grid.SetColumn(cellLabel, column);
gridChildren.Add(cellLabel);
return new Tuple<BoxView, Label>(cellBoxView, cellLabel);
}
public void DoubleTapCommand(object sender, EventArgs args)
{
App.Current.MainPage.DisplayAlert("Login Success", "", "Ok");
//System.Diagnostics.Debug.WriteLine("<||> NumberOfTapsRequired 1");
}
}
}
#endregion
#region Private methods
//private void OnColumnDefinitionsChanged()
//{
#endregion
using Syncfusion.ListView.XForms;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Xamarin.Forms;
using SelectionMode = Syncfusion.ListView.XForms.SelectionMode;
namespace GestureRecognizer
{
public class TapView : ContentPage
{
#region Private constants
private const int RowHeight = 49;
#endregion
SfListView listView;
#endregion
#region Constructors
public TapView()
{
TapModel viewModel = new TapModel();
TapViewModel tapViewModel = new TapViewModel();
listView = new SfListView();
listView.ItemsSource = viewModel.Products;
listView.ItemTemplate = new DataTemplate(() =>
{
Grid bodyLayout = new Grid
{
ColumnSpacing = 0,
RowSpacing = 0,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
bodyLayout.RowDefinitions.Add(new RowDefinition { Height = RowHeight });
bodyLayout.RowDefinitions.Add(new RowDefinition { Height = 1 });
Grid checkLayout = new Grid
{
ColumnSpacing = 0,
RowSpacing = 0,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
for (int i = 0; i < 4; i++)
{
AddCell(checkLayout.Children, i, nameof(Product.Number));
}
Grid.SetRow(checkLayout, 0);
bodyLayout.Children.Add(checkLayout);
BoxView separatorBoxView = new BoxView
{
BackgroundColor = Color.Gray,
Margin = 0
};
Grid.SetRow(separatorBoxView, 1);
bodyLayout.Children.Add(separatorBoxView);
// Single tap
TapGestureRecognizer gestureRecognizer = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, nameof(TapViewModel.TapCommand));
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
bodyLayout.GestureRecognizers.Add(gestureRecognizer);
return bodyLayout;
});
Content = listView;
}
private static Label CreateLabel(string automationId = null)
{
Label label = new Label
{
HorizontalTextAlignment = TextAlignment.Center,
FontSize = 15,
LineBreakMode = LineBreakMode.WordWrap,
Margin = new Thickness(10, 0, 10, 0),
VerticalTextAlignment = TextAlignment.Center,
AutomationId = automationId,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
return label;
}
private static BoxView CreateBoxView(int column, string automationId = null)
{
BoxView boxView = new BoxView
{
BackgroundColor = column % 2 == 0 ? Color.White : Color.Gray,
AutomationId = automationId
};
return boxView;
}
private static Tuple<BoxView, Label> AddCell(ICollection<View> gridChildren, int column,
string textBindingProperty)
{
BoxView cellBoxView = CreateBoxView(column);
Grid.SetColumn(cellBoxView, column);
gridChildren.Add(cellBoxView);
Label cellLabel = CreateLabel(textBindingProperty);
cellLabel.SetBinding(Label.TextProperty, textBindingProperty);
Grid.SetColumn(cellLabel, column);
gridChildren.Add(cellLabel);
return new Tuple<BoxView, Label>(cellBoxView, cellLabel);
}
}
}
public class TapViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public ICommand TapCommand { protected set; get; }
public TapViewModel()
{
// configure the TapCommand with a method
TapCommand = new Command(OnTapped);
}
public void OnTapped()
{
App.Current.MainPage.DisplayAlert("Login Success", "", "Ok");
}
}
I am not able to bind TapViewModel.TapCommand to the bodyLayout.
Please help me with a solution.
Sign in to answer