Hello,
You can do this by implement INotifyPropertyChanged
interface for your GroceryBO.cs
firstly. Then, please achieve OnPropertyChanged
method as well for your properies,
I make a test for PricePerItemVisibility
and PricePerPackVisibility
,
you can achieve OnPropertyChanged
method for other properties, please refer to the following code.
public class GroceryBO: INotifyPropertyChanged {
public int ItemID { get; set; }
public string ItemName { get; set; }
public string ItemDescription { get; set; }
public string ItemCoverImage { get; set; }
public double PricePerItem { get; set; }
public double PricePerPack { get; set; }
bool pricePerItemVisibility=true, pricePerPackVisibility=false;
public bool PricePerItemVisibility
{
set
{
if (pricePerItemVisibility != value)
{
pricePerItemVisibility = value;
OnPropertyChanged("PricePerItemVisibility");
}
}
get
{
return pricePerItemVisibility;
}
}
public bool PricePerPackVisibility
{
set
{
if (pricePerPackVisibility != value)
{
pricePerPackVisibility = value;
OnPropertyChanged("PricePerPackVisibility");
}
}
get
{
return pricePerPackVisibility;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
If you achieve the mvvm, you can operate object directly, no need to get all of items in the observablecollection
then re-set it, I change your code in PriceToggle_Toggled
method, you can change the value of object directly.
private void PriceToggle_Toggled(object sender, ToggledEventArgs e)
{
try
{
var swx = sender as Switch;
var x = (GroceryBO)swx.BindingContext;
if (e.Value)
{
x.PricePerItemVisibility = false;
x.PricePerPackVisibility = true;
}
else
{
x.PricePerItemVisibility = true;
x.PricePerPackVisibility = false;
}
}
catch (Exception ex)
{ }
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.