namespace ListViewXamarin
{
public class IndexToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var listview = parameter as SfListView;
var index = listview.DataSource.DisplayItems.IndexOf(value);
if (index % 2 == 0)
return Color.LightGray;
return Color.Aquamarine;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
} |
namespace ListViewXamarin
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
listView.DragDropController.UpdateSource = true;
}
private void ListView_ItemDragging(object sender, ItemDraggingEventArgs e)
{
if (e.Action == DragAction.Drop)
{
Device.BeginInvokeOnMainThread(() =>
{
if (e.NewIndex < e.OldIndex)
{
listView.RefreshListViewItem(-1, -1, true);
}
});
}
}
}
} |
<syncfusion:SfListView x:Name="listView" ItemsSource="{Binding ContactsInfo}" ItemSize="50" DragStartMode="OnHold" ItemDragging="ListView_ItemDragging">
<syncfusion:SfListView.ItemTemplate>
<DataTemplate>
<Grid Padding="10,0,0,0"
BackgroundColor="{Binding ., Converter={StaticResource IndexToColorConverter},
ConverterParameter={x:Reference Name=listView}}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label LineBreakMode="NoWrap" VerticalTextAlignment="End" Text="{Binding ContactName}"/>
<Label Grid.Row="1" VerticalTextAlignment="Start" Text="{Binding ContactNumber}"/>
</Grid>
</DataTemplate>
</syncfusion:SfListView.ItemTemplate>
</syncfusion:SfListView> |