private void ListView_OnSelectionChanged(object sender, ItemSelectionChangedEventArgs e) { for (int i = 0; i < e.AddedItems.Count; i++) { var item = e.AddedItems[i]; (item as MusicInfo).IsSelected = true; } for (int i = 0; i < e.RemovedItems.Count; i++) { var item = e.RemovedItems[i]; (item as MusicInfo).IsSelected = false; } } |
public class SelectionBoolToBackGroundColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value == true ? Color.Blue : Color.White; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } |
<sync:SfListView x:Name="listView" SelectionGesture="Tap" SelectionMode="Single" AutoFitMode="Height" SelectionChanged="ListView_OnSelectionChanged" ItemSize="70"> <sync:SfListView.ItemTemplate> <DataTemplate x:Name="ItemTemplate" x:Key="ItemTemplate"> <Grid x:Name="grid" RowSpacing="0" ColumnSpacing="0" BackgroundColor="{Binding Path=IsSelected, Converter={StaticResource BoolToColorConverter}}"> … </Grid> </DataTemplate> </sync:SfListView.ItemTemplate> </sync:SfListView> |
Hi,
this helped me to understand my problem. Thanks for the example. Any chance we can some day change the default behavior so that selecting a item does replace a defined background color? No more custom code needed that way