Hello,
I have ListView like this
<StackLayout Spacing="0"> <ListView x:Name="lvMenu" SeparatorColor="Green"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Padding="10"> <Label HorizontalOptions="FillAndExpand" VerticalOptions="Center" Text="{Binding Title}" TextColor="#03a9f4" /> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>
Also have ItemSelected event handler like this
lvMenu.ItemSelected += GotoPage; private void GotoPage(object sender, SelectedItemChangedEventArgs e) { var item = e.SelectedItem as MasterPageItem; if (item != null) { //Change bg if (item.Title == "Logout") { LogoutUser(); } else { NavigationPage nPage = new NavigationPage((Page)Activator.CreateInstance(item.TargetType)) { Title = item.Title }; this.Detail = nPage; this.IsPresented = false; } } } public class MasterPageItem { public string Title { get; set; } public Type TargetType { get; set; } }
Can anyone suggest how to change the selected item's default background color?
@TathagataBasu why don't you try like this ! Add TapGestureRecognizer for label rather than item selected event..
<Label HorizontalOptions="FillAndExpand" VerticalOptions="Center" Text="{Binding Title}" TextColor="#03a9f4"> <Label.GestureRecognizers> <TapGestureRecognizer Tapped="OnLabelClicked"/> </Label.GestureRecognizers> </Label>
in .cs file
private async void OnLabelClicked(object sender, EventArgs e) { try { var entity =((Label)sender); entity.BackgroundColor = yourcolor; var item = (MasterPageItem) entity.BindingContext; ......... } }
or else Bind background color for label by your condition.
Answers
My solution are custom renderers.
Android:
IOS:
BTW - full width separator, at iOS, as by OOTB it's not full width:
Hello Namyslaw,
Thanks for your quick answer, but I am looking for generic solution, not a platform specific solution or renderer. Can you please provide any solution in portable code?
@TathagataBasu why don't you try like this ! Add TapGestureRecognizer for label rather than item selected event..
in .cs file
or else Bind background color for label by your condition.
Thanks @Mounika.Kola for the solution, works perfectly
Btw, I need to reset the background color if another item is selected from that list. Any quick solution?
Okay can you share any screenshot for better understanding.
Please check this @Mounika.Kola
Try this once
Hi @Mounika.Kola ,
Got this error --
'ListView' does not contain a definition for 'GetChildAt'....
Can you share your skype please?
See this
https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/customizing-cell-appearance/
https://developer.xamarin.com/samples/xamarin-forms/WorkingWithListview/
we can get default selected color for list view item or else you can reload listview ItemSource data.
@TathagataBasu
Below sample is solution for your issue. Try once
Hi @Mounika.Kola ,
Thanks for your great help.
Everything are in place now.
Well
@TathagataBasu why don't you try like this ! Add TapGestureRecognizer for label rather than item selected event..
in .cs file
or else Bind background color for label by your condition.In my sample I done by setting color for label.
@TathagataBasu why don't you try like this ! Add TapGestureRecognizer for label rather than item selected event..
in .cs file
or else Bind background color for label by your condition.In my sample I done by setting color for label.
Thank you, it was really useful. If someone knows the native environments it is the best way, I think.
Thanks @Mounika.Kola
That is perfect clue.
@pratiuskumardubey
Thanks
@NamyslawSzymaniuk Thank you very much for the full width separator iOS code
I think your solution is very good!
Do you know how to implement CachingStrategy=RecycleElements in a custom ListView?
Thanks @Mounika.Kola
Your solution is very simple and working like charm
Thanks @Mounika.Kola but its solution are not working properly with StackLayout because we are not changeing StackLayout Previous background color.
public class MenuCell : ViewCell
{
public string Text
{
get { return label.Text; }
set { label.Text = value; }
}
Label label;
Mounika, how can I change the background color of a line in ListView when clicking a image with TagGestureRecogniser?
Best Regards,
Pedro
Is there any way to change cell highlight color for WPF ?
@NamyslawSzymaniuk
Hii, its not working properly.
@KolaMounika Thank so much for answering the question you made it so much simpler to fix my problem.
@rman0501 Sounds good