Skip to content

Commit b6ad9bc

Browse files
added ForceClose and ForceOpen commands
1 parent 4087629 commit b6ad9bc

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

ContextMenu/BaseContextMenuView.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Threading.Tasks;
44
using System.ComponentModel;
5+
using System.Windows.Input;
56

67
namespace ContextMenu
78
{
@@ -25,6 +26,10 @@ public abstract class BaseContextMenuView : ScrollView
2526

2627
public static readonly BindableProperty IsAutoCloseEnabledProperty = BindableProperty.Create(nameof(IsAutoCloseEnabled), typeof(bool), typeof(BaseContextMenuView), true);
2728

29+
public static readonly BindableProperty ForceCloseCommandProperty = BindableProperty.Create(nameof(ForceCloseCommand), typeof(ICommand), typeof(BaseContextMenuView), null, BindingMode.OneWayToSource);
30+
31+
public static readonly BindableProperty ForceOpenCommandProperty = BindableProperty.Create(nameof(ForceOpenCommand), typeof(ICommand), typeof(BaseContextMenuView), null, BindingMode.OneWayToSource);
32+
2833
public event Action<BaseContextMenuView> ContextMenuOpened;
2934
public event Action<BaseContextMenuView> ContextMenuClosed;
3035
public event Action<BaseContextMenuView> TouchStarted;
@@ -50,6 +55,9 @@ protected BaseContextMenuView()
5055
};
5156

5257
Scrolled += OnScrolled;
58+
59+
ForceCloseCommand = new Command(parameter => ForceClose(parameter is bool boolean ? boolean : true));
60+
ForceOpenCommand = new Command(parameter => ForceOpen(parameter is bool boolean ? boolean : true));
5361
}
5462

5563
private bool IsContextChanged { get; set; }
@@ -78,6 +86,18 @@ public bool IsAutoCloseEnabled
7886
set => SetValue(IsAutoCloseEnabledProperty, value);
7987
}
8088

89+
public ICommand ForceCloseCommand
90+
{
91+
get => (ICommand)GetValue(ForceCloseCommandProperty);
92+
set => SetValue(ForceCloseCommandProperty, value);
93+
}
94+
95+
public ICommand ForceOpenCommand
96+
{
97+
get => (ICommand)GetValue(ForceOpenCommandProperty);
98+
set => SetValue(ForceOpenCommandProperty, value);
99+
}
100+
81101
public void ForceClose(bool animated = true)
82102
=> ForceCloseContextMenu(this, animated);
83103

ContextMenuSample/ContextMenuSample/Pages/MoveToDeletePage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<CollectionView.ItemTemplate>
2727
<DataTemplate>
2828
<context:SwipeActionContextHolder MovedCommand="{Binding BindingContext.DeleteCommand, Source={x:Reference CollectionView}}">
29-
<context:SwipeActionContextMenuView>
29+
<context:SwipeActionContextMenuView ForceCloseCommand="{Binding ForceCloseCommand}">
3030
<context:SwipeActionContextMenuView.View>
3131
<Frame BackgroundColor="#512DA8"
3232
Margin="15,5"

ContextMenuSample/ContextMenuSample/Pages/SideMenuPage.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
BackgroundColor="White">
2929
<CollectionView.ItemTemplate>
3030
<DataTemplate>
31-
<context:SideContextMenuView IsAutoCloseEnabled="true">
31+
<context:SideContextMenuView IsAutoCloseEnabled="true"
32+
ForceCloseCommand="{Binding ForceCloseCommand}">
3233
<context:SideContextMenuView.View>
3334
<Frame BackgroundColor="#512DA8"
3435
Margin="15,5"

ContextMenuSample/ContextMenuSample/ViewModels/BaseItemsViewModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public class BaseItemsViewModel : BaseViewModel
1111
private ICommand _deleteCommand;
1212
public ICommand DeleteCommand => _deleteCommand ?? (_deleteCommand = new Command(item =>
1313
{
14-
Items.Remove(item as Item);
14+
var model = item as Item;
15+
model?.ForceCloseCommand?.Execute(false); // OneWayToSource binding. So we call Close method without animation
16+
Items.Remove(model);
1517
}));
1618

1719
private ICommand _muteCommand;
@@ -63,6 +65,8 @@ public sealed class Item : BaseViewModel
6365
private string _name;
6466
private bool _isMuted;
6567

68+
public ICommand ForceCloseCommand { get; set; }
69+
6670
public string AvatarUrl
6771
{
6872
get => _avatarUrl;

0 commit comments

Comments
 (0)