Hello,
Welcome to Microsoft Q&A!
It can be easily achieved if we create a bool property named Selected
inside ViewModel , and implement INotifyPropertyChanged
.
public class ViewModel : INotifyPropertyChanged
{
//xxxxxx
private bool selected;
public bool Selected { get {
return selected;
} set {
selected = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Create binding on the BackgroundColor of the cardView with Selected
, and of course we need to use a Converter .
<ContentPage.Resources>
<ResourceDictionary>
<local:MyConverter x:Key="ccc" />
</ResourceDictionary>
</ContentPage.Resources>
<grial:CardView BackgroundColor="{Binding Selected,Converter={StaticResource ccc}}">
At last in tap event we need to highlight the selected one and reset the previous one .
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
var model = ((StackLayout)sender).BindingContext as ViewModel;
if(model.Selected == false)
{
model.Selected = true;
foreach(var item in CurrentFloors)
{
if(item != model)
{
item.Selected = false;
}
}
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
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.