Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Core] SwipeItem Parent using SwipeView in DataTemplate #13385

Merged
merged 7 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="using:Xamarin.Forms.Controls"
xmlns:issues="using:Xamarin.Forms.Controls.Issues"
mc:Ignorable="d"
Title="Test 13232"
x:Class="Xamarin.Forms.Controls.Issues.Issue13232">
<local:TestContentPage.Resources>
<ResourceDictionary>

<DataTemplate x:Key="SwipeItemDataTemplate" x:DataType="issues:Issue13232Model">
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem
BackgroundColor="Red"
Text="Delete"
Command="{Binding DeleteCommand, Source={RelativeSource AncestorType={x:Type issues:Issue13232ViewModel}}}"
CommandParameter="{Binding .}" />
</SwipeItems>
</SwipeView.RightItems>
<StackLayout
BackgroundColor="White">
<StackLayout
Orientation="Horizontal">
<Label
Text="Swipe to Left"
FontSize="Subtitle"/>
</StackLayout>
<Label
Text="{Binding Title}"
FontSize="Micro" />
<Label
Text="{Binding SubTitle}"
FontSize="Micro" />
</StackLayout>
</SwipeView>
</DataTemplate>

</ResourceDictionary>
</local:TestContentPage.Resources>
<Grid
RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label
Padding="12"
BackgroundColor="Black"
TextColor="White"
Text="Swipe to delete the message, if the Command is working, the test has passed."/>
<CollectionView
Grid.Row="1"
ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource SwipeItemDataTemplate}"
BackgroundColor="LightGray"
ItemSizingStrategy="MeasureAllItems"
ItemsUpdatingScrollMode="KeepLastItemInView" />
</Grid>
</local:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 13232,
"[Bug] Command is not working in SwipeItem",
PlatformAffected.Android | PlatformAffected.iOS)]
public partial class Issue13232 : TestContentPage
{
public Issue13232()
{
#if APP
InitializeComponent();
BindingContext = new Issue13232ViewModel();
#endif
}

protected override void Init()
{

}
}

[Preserve(AllMembers = true)]
public class Issue13232Model
{
public int Id { get; set; }
public string Title { get; set; }
public string SubTitle { get; set; }
}

[Preserve(AllMembers = true)]
public class Issue13232ViewModel : BindableObject
{
public Issue13232ViewModel()
{
Items = new ObservableCollection<Issue13232Model>();

LoadItems();

DeleteCommand = new Command(async (object model) =>
{
await Task.Delay(100);

var issue13232Model = (Issue13232Model)model;

var itemToDelete = Items.SingleOrDefault(t => t.Id == issue13232Model.Id);

if (itemToDelete != null)
{
await Task.Delay(500);

Items.Remove(itemToDelete);
}
});
}

public ObservableCollection<Issue13232Model> Items { get; set; }

public ICommand DeleteCommand { get; }

void LoadItems()
{
for (int i = 0; i < 10; i++)
{
Items.Add(new Issue13232Model
{
Id = i + 1,
Title = $"Message {i + 1}",
SubTitle = $"Description {i + 1}",
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,7 @@
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutBackground.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentOffest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShellFlyoutContentWithZeroMargin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13232.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13378.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13376.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LabelFormattedTextHtmlPadding.cs" />
Expand Down Expand Up @@ -2148,6 +2149,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13136.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13232.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13378.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
18 changes: 17 additions & 1 deletion Xamarin.Forms.Core/SwipeView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms.Platform;

namespace Xamarin.Forms
Expand Down Expand Up @@ -117,6 +116,23 @@ protected override void OnBindingContextChanged()
SetInheritedBindingContext(BottomItems, bc);
}

protected override void OnParentSet()
{
base.OnParentSet();

if (LeftItems != null)
UpdateSwipeItemsParent(LeftItems);

if (RightItems != null)
UpdateSwipeItemsParent(RightItems);

if (TopItems != null)
UpdateSwipeItemsParent(TopItems);

if (BottomItems != null)
UpdateSwipeItemsParent(BottomItems);
}

SwipeItems SwipeItemsDefaultValueCreator() => new SwipeItems();

static object SwipeItemsDefaultValueCreator(BindableObject bindable)
Expand Down