Hi,
I have made a sample application and added GestureRecognizer to it, but tapping isn't working.
Could you please help me with this.
Thanks
Sonal
|
// Single tap
TapGestureRecognizer gestureRecognizer = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, new Binding() { Source = tapViewModel, Path = "TapCommand" });
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
bodyLayout.GestureRecognizers.Add(gestureRecognizer);
// Double tap
gestureRecognizer = new TapGestureRecognizer { NumberOfTapsRequired = 2 };
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, new Binding() { Source = tapViewModel, Path = "DoubleTapCommand" });
gestureRecognizer.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
bodyLayout.GestureRecognizers.Add(gestureRecognizer); |
Hi Lakshmi,
I have added a combination of Label and BoxView as items of a list.
I want the BoxView to be visible when I tap to select an item and Change its color to blue but right now I am not able to achieve this.
Could you please help.
In the below method of TapView class I want to add something as highlighted in bold:
private static Tuple<BoxView, Label> AddCell(ICollection<View> gridChildren, int column,
string textBindingProperty, string text = null, bool isBindable = true)
{
TapViewModel tap = new TapViewModel();
BoxView cellBoxView = CreateBoxView(column);
Grid.SetColumn(cellBoxView, column);
gridChildren.Add(cellBoxView);
Label cellLabel = CreateLabel(textBindingProperty);
if (isBindable)
{
cellBoxView.SetBinding(IsVisibleProperty, nameof(TapViewModel.IsSelected),
converter: new InverseBoolConverter());
cellBoxView.BindingContext = tap.IsSelected;
cellLabel.SetBinding(Label.TextColorProperty, nameof(TapViewModel.TextColor));
}
if (text == null)
cellLabel.SetBinding(Label.TextProperty, textBindingProperty);
else
cellLabel.Text = text;
Grid.SetColumn(cellLabel, column);
gridChildren.Add(cellLabel);
return new Tuple<BoxView, Label>(cellBoxView, cellLabel);
}
TapViewModel:
private bool _isSelected;
public virtual bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
BackgroundColor = SelectedBackgroundColor;
OnPropertyChanged();
}
}
The sample app is same as attached earlier.
Thanks
Sonal
|
private static Tuple<BoxView, Label> AddCell(ICollection<View> gridChildren, int column,
string textBindingProperty, string text = null, bool isBindable = true)
{
…
gridChildren.Add(cellBoxView);
Label cellLabel = CreateLabel(textBindingProperty);
if (isBindable)
{
cellBoxView.SetBinding(BoxView.IsVisibleProperty, new Binding("IsSelected"));
}
…
}
//TapViewModel
public void OnTapped(object obj)
{
(obj as Product).IsSelected = true;
App.Current.MainPage.DisplayAlert("Login Success", "", "Ok");
}
|
Hi Lakshmi,
Thank you so much for your help.
My requirement is that when I tap to select an item then the color of the selected item should be blue. Right now the background color is changing to blue.
Please see the attached image.
Thanks
Sonal
|
listView.ItemTemplate = new DataTemplate(() =>
{
Grid bodyLayout = new Grid
{
ColumnSpacing = 0,
RowSpacing = 0,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
var binding = new Binding("IsSelected");
binding.Converter = new ItemsBackgroundColorConverter();
bodyLayout.SetBinding(Grid.BackgroundColorProperty, binding);
bodyLayout.RowDefinitions.Add(new RowDefinition { Height = RowHeight });
bodyLayout.RowDefinitions.Add(new RowDefinition { Height = 1 });
…
return bodyLayout;
}); |
|
public class ItemsBackgroundColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Color.FromHex("#1572A1") : Color.Transparent;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} |