My question is about how to control the background and text color of multiple buttons when you read a button click in MVVM. To make my question a bit clear please have a look at the UI I have attached.
I have already implemented this on code behind i.e on button click, I am handling all of the buttons separately. The original background and text color are white and black respectively and when you click on any other button except 1W that button will be highlighted. In the next image, 3M is clicked
lbl3M.BackgroundColor = Color.FromHex(defaultColor);
lbl3M.TextColor = Color.White;
if (lbl1M.BackgroundColor != Color.White)
{
lbl1M.BackgroundColor = Color.White;
lbl1M.TextColor = Color.Black;
}
if (lbl1W.BackgroundColor != Color.White)
{
lbl1W.BackgroundColor = Color.White;
lbl1W.TextColor = Color.Black;
}
if (lbl6M.BackgroundColor != Color.White)
{
lbl6M.BackgroundColor = Color.White;
lbl6M.TextColor = Color.Black;
}
if (lbl1Y.BackgroundColor != Color.White)
{
lbl1Y.BackgroundColor = Color.White;
lbl1Y.TextColor = Color.Black;
}
I have done this on each button clicked.
I know this approach is not cost-effective and I want to learn how to implement in MVVM way
EDIT: I have created a function which reset all the buttons to Original UI and change the UI on button clicked
void ResetButtonUI()
{
lbl1W.BackgroundColor = Color.White;
lbl1W.TextColor = Color.Black;
lbl1M.BackgroundColor = Color.White;
lbl1M.TextColor = Color.Black;
lbl3M.BackgroundColor = Color.White;
lbl3M.TextColor = Color.Black;
lbl6M.BackgroundColor = Color.White;
lbl6M.TextColor = Color.Black;
lbl1Y.BackgroundColor = Color.White;
lbl1Y.TextColor = Color.Black;
}
and on each button i have this logic
ResetButtonUI();
button.BackgroundColor = Color.FromHex(defaultColor);
button.TextColor = Color.White;
IsSelected
boolean property to the button to handle color change logic. I think you should have a command for each button, so you can handle there theIsSelected
logic for both new selected button and previous one. Try with this approach – FabriBertani