I have a ListView with custom ViewCell, inside that ViewCell i have a label that i have attached a TapGestureRecognizer to so that i can handle the event when the user presses that label differently then when they just select the entire Cell. However I have noticed that when I get my event one of the Properties which I (think) have bound to a string value from a DataContainer class is always null. I am not sure if this is because my binding is incorrect or if I need to push my Event handling up to the parent? Code below.
This is my ViewCell
namespace Exchange.ViewModels
{
public class PersonalOrderViewCell : ViewCell
{
public static readonly BindableProperty GUIDProperty = BindableProperty.Create("GUID", typeof(string), typeof(DataContainer), null);
public string GUID
{
get { return (string)GetValue(GUIDProperty);}
set { SetValue(GUIDProperty, value); }
}
public EventArgs e = null;
public event ButtonClickEvent ClickListener;
public delegate void ButtonClickEvent(object m, EventArgs e);
public PersonalOrderViewCell(ButtonClickEvent Listener)
{
ClickListener += Listener;
StackLayout wrapper = new StackLayout();
Grid horizontal = new Grid
{
};
horizontal.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
horizontal.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
horizontal.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
horizontal.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
horizontal.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
horizontal.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
Label Amount = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.Gray};
Label Price = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.Gray };
Label Time = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.Gray };
Label Status = new Label { HorizontalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.Gray,};
//Bindings
Amount.SetBinding(Label.TextProperty, "Amount");
Amount.SetBinding(Label.TextColorProperty, "Color");
Price.SetBinding(Label.TextProperty, "Price");
Price.SetBinding(Label.TextColorProperty, "Color");
Time.SetBinding(Label.TextProperty, "Time");
Time.SetBinding(Label.TextColorProperty, "Color");
Status.SetBinding(Label.TextProperty, "Status");
Status.SetBinding(Label.TextColorProperty, "Color");
var tgr = new TapGestureRecognizer();
tgr.Tapped += OnButtonClicked;
Status.GestureRecognizers.Add(tgr);
//Properties
wrapper.BackgroundColor = Color.White;
horizontal.Children.Add(Amount,0,0);
horizontal.Children.Add(Price,1,0);
horizontal.Children.Add(Time,3,0);
horizontal.Children.Add(Status, 5, 0);
wrapper.Children.Add(horizontal);
View = wrapper;
}
void OnButtonClicked(object sender, EventArgs e)
{
string hold = GUID;
if (ClickListener != null)
ClickListener(sender, e);
}
}
This is the Container Class
namespace Exchange.Data
{
class DataContainer : INotifyPropertyChanged
{
private bool _sell;
public string Amount { get; set; }
public string Color { get; private set; }
public string Price { get; set; }
public string MyAmount { get; set; }
public string Time { get; set; }
public string Status { get; set; }
private string _GUID;
public string GUID
{
get { return _GUID; }
set
{
_GUID = value;
OnPropertyChanged("GUID");
}
}
public bool Sell
{
get { return _sell; }
set
{
_sell = value;
if (_sell)
{
Color = "Red";
}
else
{
Color = "Green";
}
}
}
This is the Page that I am using it in
namespace Exchange.Pages
{
public class PersonalOrderPage
{
public StackLayout Layout { get; private set; }
public PersonalOrderViewCell.ButtonClickEvent OrderButtonHandler { get; set; }
public EventHandler<SelectedItemChangedEventArgs> OrderSelectedHandler { get; set; }
public EventHandler CancelAllHandler { get; set; }
private ListView list;
public PersonalOrderPage(PersonalOrderViewCell.ButtonClickEvent OrderButtonHandler, EventHandler<SelectedItemChangedEventArgs> OrderSelectedHandler,
EventHandler CancelAllHandler)
{
this.OrderButtonHandler = OrderButtonClicked;
this.OrderSelectedHandler = OrderSelectedHandler;
this.CancelAllHandler = CancelAllHandler;
SetLayout();
}
private void OrderButtonClicked(object m, EventArgs e)
{
//TODO: get GUID from this method so I can update list
}
private void SetLayout()
{
StackLayout wrapper = new StackLayout();
list = new ListView();
list.HasUnevenRows = true;
Grid titlesGrid = new Grid { };
ObservableCollection<DataContainer> orders = new ObservableCollection<DataContainer>();
//TODO: change this to a image
//Cancel All Button
Button cancelAllButton = new Button { Text = "Cancel All", HorizontalOptions = LayoutOptions.FillAndExpand, FontSize = 8 };
cancelAllButton.Clicked += CancelAllHandler;
//Title Grid
titlesGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
titlesGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
titlesGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
titlesGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
titlesGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
titlesGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
titlesGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
titlesGrid.Children.Add(new Label { Text = "Amount" }, 0, 0);
titlesGrid.Children.Add(new Label { Text = "Price" }, 2, 0);
titlesGrid.Children.Add(new Label { Text = "Time" }, 4, 0);
titlesGrid.Children.Add(cancelAllButton, 5, 0);
//Set to custom view cell
list.ItemTemplate = new DataTemplate(() => new PersonalOrderViewCell(OrderButtonHandler));
orders.Add(new DataContainer { Amount = "1.0", Price = "10", Time = "9:00pm", Status = "open", Sell = true, GUID = "01010" });
orders.Add(new DataContainer { Amount = "1.0", Price = "70.0", Time = "9:00pm", Status = "open", Sell = false, GUID = "01010" });
orders.Add(new DataContainer { Amount = "1.0", Price = "200", Time = "9:00pm", Status = "open", Sell = true, GUID = "01010" });
list.ItemsSource = orders;
list.ItemSelected += OrderSelectedHandler;
wrapper.Children.Add(titlesGrid);
wrapper.Children.Add(list);
Layout = wrapper;
}
}