XamarinCommunityToolkit icon indicating copy to clipboard operation
XamarinCommunityToolkit copied to clipboard

[Bug] TabView: BindingContext is not correctly initialized on custom controls

Open StepKie opened this issue 4 years ago • 3 comments

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

  1. Clone sample repository
  2. 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

StepKie avatar Apr 30 '21 19:04 StepKie

@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.

angelru avatar May 04 '21 14:05 angelru

Any updates on how to work around?

mikkelAtVisma avatar Jul 30 '21 10:07 mikkelAtVisma

This is still happening for me - will this ever be addressed?

StepKie avatar Feb 18 '22 15:02 StepKie