Hi,
I am using listview in xamarin forms.
It has the viewcell function like this
public class VetCell : ViewCell
{
public ViewCellModel ViewCell;
public Label SerialNum = new Label();
public Label PLUNumber = new Label();
public Label PLUDescription = new Label();
public Label Price = new Label();
public Image menuImage = new Image();
public Grid ViewCellGrid;
public Grid LastGrid;
public Label SerialNum4 = new Label();
public Label SerialNum5 = new Label();
public Label SerialNum6 = new Label();
public Frame FrameLayout = new Frame();
public VetCell():base()
{
ViewCell = new ViewCellModel() { Color1=Color.Pink };
BindingContext = ViewCell;
var nameLayout = CreateCellLayout();
var viewLayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
Children = { nameLayout }
};
viewLayout.SetBinding(Layout.BackgroundColorProperty, new Binding("Color"));
View = viewLayout;
}
StackLayout CreateCellLayout()
{
ViewCellGrid = new Grid();
ViewCellGrid.RowDefinitions = new RowDefinitionCollection();
ViewCellGrid.ColumnDefinitions = new ColumnDefinitionCollection();
ViewCellGrid.RowDefinitions.Add(new RowDefinition { Height=new GridLength(1,GridUnitType.Star) });
ViewCellGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
ViewCellGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
ViewCellGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Star) });
ViewCellGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
ViewCellGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
ViewCellGrid.Padding = new Thickness(1);
ViewCellGrid.RowSpacing = 0.5;
ViewCellGrid.ColumnSpacing = 0.5;
ViewCellGrid.HorizontalOptions = LayoutOptions.FillAndExpand;
SerialNum = new Label
{
FontSize = 25,
FontAttributes = FontAttributes.None,
HorizontalOptions = LayoutOptions.FillAndExpand,
BackgroundColor=Color.White,
TextColor=Color.Gray,
XAlign=TextAlignment.Center
};
SerialNum.SetBinding(Label.TextProperty, new Binding("SerialNo"));
PLUNumber = new Label
{
FontSize = 25,
FontAttributes = FontAttributes.None,
BackgroundColor = Color.White,
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black,
XAlign = TextAlignment.Center
};
PLUNumber.SetBinding(Label.TextProperty, new Binding("PLUNumber"));
PLUDescription = new Label
{
FontSize = 25,
FontAttributes = FontAttributes.None,
BackgroundColor = Color.White,
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black,
XAlign = TextAlignment.Center
};
PLUDescription.SetBinding(Label.TextProperty, new Binding("PLUDescription"));
Price = new Label
{
FontSize = 25,
FontAttributes = FontAttributes.None,
BackgroundColor = Color.White,
HorizontalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black,
XAlign = TextAlignment.Center
};
Price.SetBinding(Label.TextProperty, new Binding("Price"));
LastGrid = new Grid();
LastGrid.HorizontalOptions = LayoutOptions.FillAndExpand;
LastGrid.BackgroundColor = Color.FromHex("#e6e6e6");
//LastGrid.SetBinding(Grid.BackgroundColorProperty, new Binding("Color"));
LastGrid.RowDefinitions = new RowDefinitionCollection();
LastGrid.ColumnDefinitions = new ColumnDefinitionCollection();
LastGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
LastGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
LastGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
LastGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
SerialNum4 = new Label
{
Text = "$",
TextColor = Color.Black,
HorizontalOptions =LayoutOptions.FillAndExpand,
BackgroundColor=Color.Pink,
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center
};
// SerialNum4.SetBinding(Label.BackgroundColorProperty,new Binding("Color"));
SerialNum5 = new Label
{
Text = "%",
TextColor = Color.Black,
HorizontalOptions = LayoutOptions.FillAndExpand,
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center
};
//SerialNum5.BackgroundColor = Color.Pink;
SerialNum5.SetBinding(Label.BackgroundColorProperty, new Binding("Color, Converter ={ StaticResource LabelBackGroundConverter }"));
SerialNum6 = new Label
{
Text = "New",
TextColor = Color.Black,
HorizontalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.Pink,
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center
};
// SerialNum6.SetBinding(Label.BackgroundColorProperty, new Binding("Color"));
LastGrid.Children.Add(SerialNum4, 0, 0);
LastGrid.Children.Add(SerialNum5, 0, 1);
LastGrid.Children.Add(SerialNum6, 0, 2);
ViewCellGrid.Children.Add(SerialNum, 0, 0);
ViewCellGrid.Children.Add(PLUNumber, 1, 0);
ViewCellGrid.Children.Add(PLUDescription, 2, 0);
ViewCellGrid.Children.Add(Price, 3, 0);
ViewCellGrid.Children.Add(LastGrid, 4, 0);
var nameLayout = new StackLayout()
{
HorizontalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Horizontal,
Children = { ViewCellGrid },
};
return nameLayout;
}
}
I have an event which gets called view model is the page view model
void OnTapGestureRecognizerTapped(object sender, EventArgs e)
{
viewModel.PLUDetails.PLUDescription = "XYZ";
}
The view model is :
public class CalculatorViewModel:INotifyPropertyChanged
{
public SalesRegistrationViewModel SalesviewModel
{
get { return _salesviewModel; }
set
{
if (_salesviewModel != value)
{
_salesviewModel = value;
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs("SalesviewModel"));
}
}
}
}
public PLUDetails PLUDetails
{
get { return _pludetails; }
set
{
if (_pludetails != value)
{
_pludetails = value;
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs("PLUDetails"));
}
}
}
}
private PLUDetails _pludetails;
private SalesRegistrationViewModel _salesviewModel;
public event PropertyChangedEventHandler PropertyChanged;
}
The PLUDetails class is:
public class PLUDetails:INotifyPropertyChanged
{
public int SerialNo { get; set; }
public string PLUNumber { get; set; }
public string PLUDescription
{
get { return pludescription; }
set
{
if (pludescription != value)
{
pludescription = value;
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs("PLUDescription"));
}
}
}
}
public double Price
{
get { return price; }
set
{
if (price != value)
{
price = value;
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs("Price"));
}
}
}
}
private string pludescription;
private double price;
public event PropertyChangedEventHandler PropertyChanged;
public string DiscountType { get; set; }
}
But my label is not getting updated. It is showing blank. Kindly help me in this. I am frustrated.