ListView not unselecting

后端 未结 4 946
北海茫月
北海茫月 2021-01-14 23:19

I am working on a Xamarin.Forms project with a ListView.

The XAML for the ListView is



        
4条回答
  •  伪装坚强ぢ
    2021-01-14 23:38

    I prefer to use the SelectedItem property to handle simple clicks. This works platform independent and everything can be done in the view model code.

    The trick is to set the property null again to deselect the item immediately after evaluating it (in hte sample I use it for initialising another view model).

    XAML:

    
    

    ViewModel.cs:

    public Command ViewDetailsCommand;
    public ViewModel()
    {
         ViewDetailsCommand = new Command(async s => await ViewDetails(s));
    }
    
    public IProduct SelectedProduct
    {
        get { return _selectedProduct; }
        set
        {
            if (value != _selectedProduct)
            {
                SetProperty(ref _selectedProduct, value);
                if (value != null)
                {
                    ViewDetailsCommand.Execute(value);
                }
            }
        }
    }
    
    private async Task ViewDetails(IProduct product)
    {
        var viewModel = AppContainer.Container.Resolve();
        viewModel.Initialise(this, product as ShoppingListItemViewModel);
        SelectedProduct = null;
        await _pageNavigator.PushModalAsync(viewModel);
    }
    

提交回复
热议问题