Xamarin.forms: [Bug] Binding SwipeView IsEnabled Property Doesn't Work (iOS & Android)

Created on 24 Nov 2020  路  3Comments  路  Source: xamarin/Xamarin.Forms

Description

I'm trying to control SwipeView programmatically so that based on specific conditions it gets enabled/disabled. Binding the IsEnabled property doesn't seem to work.

Note that hardcoding IsEnabled to "True/False" in the xaml does work as expected. It misbehaves when binded to a value.

Steps to Reproduce

Bind SwipeView IsEnabled property.

Expected Behavior

SwipeView should bind to the property and act accordingly i.e. when IsEnabled = False, it should not open when swiped.

Actual Behavior

It ignores the binding value.

Basic Information

  • Version with issue: XF4.8 & XF5.x (Pre)
  • Last known good version: Don't know
  • Platform Target Frameworks:

    • iOS: 14.2

    • Android: 11

  • Affected Devices: Emulators

Environment

Microsoft Visual Studio Community 2019 Version 16.8.2

Screenshots

ezgif com-gif-maker (3)

Reproduction Link

https://github.com/darrabam/SwipeBindingBug

Workaround

Don't know.

unverified bug

All 3 comments

Check your console output - I'm betting there's an error in there somewhere like "Property IsSwipeViewEnabled not found".

Your items in your CollectionView are bound to objects of type String (in your PageViewModel's Items collection). So all those bindings in your DataTemplate are targeting a String object, not the PageViewModel object which contains your IsSwipeViewEnabled property.

If you want the IsEnabled property of the SwipeView in your DataTemplate to target that property on the PageViewModel object, you'll need to use a relative source in your binding. So on your SwipeView, you want the IsEnabled property to look something like this:

IsEnabled="{Binding Source={RelativeSource AncestorType={x:Type local:PageViewModel}}, Path=IsSwipeViewEnabled}}"

That tells the binding to work its way up through the hierarchy to the PageViewModel and use that for binding this particular property.

(Don't forget to declare the local namespace so the XAML knows what a PageViewModel is - inside your ContentPage tag, add xmlns:local="clr-namespace:SwipeBindingBug.ViewModels".)

@hartez Many thanks for your prompt reply and clear explanation! You are absolutely correct! I don鈥檛 know why I misunderstood swipeview as a page element and not part of the collection / data template scope.

Thanks again,

Was this page helpful?
0 / 5 - 0 ratings