XamarinCommunityToolkit
XamarinCommunityToolkit copied to clipboard
[Bug] TabView: BindingContext is not correctly initialized on custom controls
Description
TabView does not initialize BindingContext on custom controls correctly. Weirdly, it works when wrapping it in an additional standard control. See sample repository: https://github.com/Hottemax/XamarinTabViewBug
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="XctTabViewBug.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:XctTabViewBug"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
<ContentPage.BindingContext>
<vm:MainPageViewModel />
</ContentPage.BindingContext>
<Grid>
<xct:TabView>
<xct:TabViewItem Text="Ok">
<!-- No idea why we need to wrap in another content view, otherwise binding does not work -->
<ContentView>
<vm:CustomView BindingContext="{Binding Cvm}" />
</ContentView>
</xct:TabViewItem>
<xct:TabViewItem Text="Buggy">
<vm:CustomView BindingContext="{Binding Cvm}" />
</xct:TabViewItem>
</xct:TabView>
</Grid>
</ContentPage>
Steps to Reproduce
- Clone sample repository
- Start the app (Android)
Expected Behavior
Binding should work for the "Buggy" tab
Actual Behavior
It does not, it only works in the "Ok" tab where the custom view is wrapped in another standard control (ContentView in the sample)
Basic Information
-
Version with issue:
-
Last known good version:
-
IDE: VS 2019 16.9
-
Platform Target Frameworks:
-
Nuget Packages: Xamarin Community Toolkit
Reproduction Link
https://github.com/Hottemax/XamarinTabViewBug
@Hottemax
I am facing the same problem, it doesn't even work if the ViewModel is initialized next to the view, it only works if the ViewModel is already initialized in the MainPageViewModel, as in your sample.
Any updates on how to work around?
This is still happening for me - will this ever be addressed?