Listview's SelectedItem property raise command is disable when using ViewCell.ContextAction MenuItem

ThinhTranCThinhTranC Member ✭✭

Hi I have a listview, and as I'm using freshmvvm to use MVVM. I can't not call ItemSelected or ItemTapped event to raise the command in the viewmodel. Instead I have to use "SelectedItem" to raise property change. My xaml Listview look like this :smile:

     <ListView 
                          Margin="20, 0"
                          HorizontalOptions="FillAndExpand"
                          ItemsSource="{Binding Cases}"
                          SelectedItem="{Binding SelectedCase}"
                          HasUnevenRows="true"
                          IsPullToRefreshEnabled="True"
                          RefreshCommand="{Binding PullRefreshCases}"
                          IsRefreshing="{Binding IsRefreshing}"
                          >
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell Height="70">
                                <ViewCell.ContextActions>
                                    <MenuItem Command="{Binding Path = BindingContext.DeleteCaseCommand, Source={x:Reference thisCasePage}}" 
                                                                                                                         CommandParameter="{Binding .}"  Text="Delete" IsDestructive="True" />
                                    <MenuItem Command="{Binding Path = BindingContext.OpenMapCommand, Source={x:Reference thisCasePage}}"                            
                                                                                         CommandParameter="{Binding .}"  Text="Map"/>
                                </ViewCell.ContextActions>
                   </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
       </ListView>

In my viewmodel, I have:

        private Case _selectedCase;

        public Case SelectedCase
        {
            get { return _selectedCase; }
            set {
                _selectedCase = value;
                if (CaseSelected.CanExecute(value))
                    CaseSelected.Execute(value);
            }
        }

        public Command<Case> CaseSelected
        {
            get
            {
                return new Command<Case>(async (thisCase) =>
               {
                   await CoreMethods.PushPageModel<CaseDetailPageModel>(thisCase);
               });
            }
        }

This is working fine, and I can triggger the "CaseSelected" command when I selected an item in the listview.

However, I encountered a bug. As you can see I have View.ContextActions MenuItem in the listview as well, so that I can swipe right to left (on IOS) to display buttons.
The bug is that as soon as I swipe the item in the listview (even with/without pressing any button just swipe back and forward left right), after that I can no longer trigger the "CaseSelected" command when selecting an item in the listview.

I went through the listview sample on the xamarin samples code. But it seems that they use ItemTapped/ItemSelected in code behind.

Can anyone be able to tell me what causes me the bug and how to work around it ?
Alternatively, I want to try if ItemTapped/ItemSelected event raise will fix the issue. But I don't want to use code behind. And I can't bind a command to these. I've seen some people suggested of using Xamarin.Forms.Behaviors to do this, can anyone be able to give me an example?

I checked this https://anthonysimmon.com/eventtocommand-in-xamarin-forms-apps/
But it seems quite old, and not as up to date, and still not quite clear to me on how to use it.

Thanks

Tagged:

Best Answers

Answers

Sign In or Register to comment.