<sfgrid:SfDataGrid x:Name="listItems" ScrollingMode="PixelLine" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All"
ItemsSource="{Binding WorkoutSetWeightLogs}"
VerticalOptions="FillAndExpand" RowHeight="100"
AutoGenerateColumns="False"
AllowSorting="False"
IsEnabled="True"
ColumnSizer="Star"
AllowSwiping="True"
AllowDraggingRow="True"
AllowDraggingColumn="False"
SelectionMode="Single"
GridTapped="dataGrid_GridTapped"
PropertyChanged="dataGrid_PropertyChanged"
SwipeEnded="dataGrid_SwipeEnded"
SwipeStarted="dataGrid_SwipeStarted"
>
<sfgrid:SfDataGrid.LeftSwipeTemplate>
<DataTemplate>
<ContentView BackgroundColor="#1AAA87" BindingContextChanged="leftTemplate_BindingContextChanged">
<Grid IsVisible="False" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width ="20" />
<ColumnDefinition Width ="*" />
<ColumnDefinition Width ="75"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="1"
FontSize="15"
LineBreakMode="NoWrap"
Text="DELETED"
TextColor="White"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center" />
<Label BindingContextChanged="label_BindingContextChanged"
Grid.Column="2"
FontSize="15"
FontAttributes="Bold"
HorizontalOptions="Center"
LineBreakMode="NoWrap"
Text="UNDO"
TextColor="White"
HorizontalTextAlignment="End"
VerticalTextAlignment="Center" />
</Grid>
</ContentView>
</DataTemplate>
</sfgrid:SfDataGrid.LeftSwipeTemplate>
<sfgrid:SfDataGrid.RightSwipeTemplate>
<DataTemplate>
<ContentView BackgroundColor="#1AAA87" BindingContextChanged="rightTemplate_BindingContextChanged">
<Grid IsVisible="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width ="20" />
<ColumnDefinition Width ="*" />
<ColumnDefinition Width ="75"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="1"
FontSize="15"
LineBreakMode="NoWrap"
Text="Deleted"
TextColor="White"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
/>
<Label Grid.Column="2"
BindingContextChanged="label_BindingContextChanged"
FontSize="15"
FontAttributes="Bold"
HorizontalOptions="Center"
LineBreakMode="NoWrap"
Text="UNDO"
TextColor="White"
HorizontalTextAlignment="End"
VerticalTextAlignment="Center" />
</Grid>
</ContentView>
</DataTemplate>
</sfgrid:SfDataGrid.RightSwipeTemplate>
<sfgrid:SfDataGrid.HeaderRowHeight>
<OnPlatform Android="50"
WinPhone="60"
iOS="50"
x:TypeArguments="x:Double" />
</sfgrid:SfDataGrid.HeaderRowHeight>
<sfgrid:SfDataGrid.Columns>
<sfgrid:GridTemplateColumn HeaderFontAttribute="Bold"
HeaderText="SETS"
MappingName="SetNumber">
<sfgrid:GridTemplateColumn.HeaderCellTextSize>
<OnIdiom Phone="12"
Tablet="15"
x:TypeArguments="x:Double" />
</sfgrid:GridTemplateColumn.HeaderCellTextSize>
<sfgrid:GridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid BackgroundColor="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="64"></RowDefinition>
</Grid.RowDefinitions>
</Grid>
</DataTemplate>
</sfgrid:GridTemplateColumn.CellTemplate>
</sfgrid:GridTemplateColumn>
</sfgrid:SfDataGrid.Columns>
</sfgrid:SfDataGrid >
private async void doDeleting(ItemsMapping deleted = null)
{
isSuspend = true;
await Task.Delay(2000);
vm = (ViewModel)this.BindingContext;
if (leftTemplateView.Content.IsVisible)
leftTemplateView.Content.IsVisible = false;
else if (rightTemplateView.Content.IsVisible)
rightTemplateView.Content.IsVisible = false;
if (!IsUndoClicked)
{
vm.DeleteSet((vm.ItemsWeightLogs[swipedRowindex - 1]));
//listItems.ItemsSource = null;
//listItems.ItemsSource = vm.ItemsWeightLogs;
}
else
{
var removedData = vm.ItemsWeightLogs[swipedRowindex - 1];
var isSelected = listItems.SelectedItems.Contains(removedData);
vm.ItemsWeightLogs.Remove(removedData);
vm.ItemsWeightLogs.Insert(swipedRowindex - 1, removedData);
if (isSelected)
listItems.SelectedItems.Add(removedData);
IsUndoClicked = false;
}
isSuspend = false;
}
private void label_BindingContextChanged(object sender, EventArgs e)
{
var label = sender as Label;
if (label.GestureRecognizers.Count == 0)
{
label.GestureRecognizers.Add(new TapGestureRecognizer() { Command = new Command(Undo) });
}
}
private void leftTemplate_BindingContextChanged(object sender, EventArgs e)
{
leftTemplateView = sender as ContentView;
}
private void rightTemplate_BindingContextChanged(object sender, EventArgs e)
{
rightTemplateView = sender as ContentView;
}
private void Undo()
{
IsUndoClicked = true;
}
private void dataGrid_SwipeStarted(object sender, SwipeStartedEventArgs e)
{
if (isSuspend)
e.Cancel = true;
}
private void dataGrid_SwipeEnded(object sender, SwipeEndedEventArgs e)
{
swipedRowindex = e.RowIndex;
if (Math.Abs(e.SwipeOffset) >= listItems.Width)
{
if (e.SwipeOffset > 0)
leftTemplateView.Content.IsVisible = true;
else
rightTemplateView.Content.IsVisible = true;
doDeleting();
}
}