Bug 35922 - a binding with x:Reference doesn't work inside the DataTemplate element
Summary: a binding with x:Reference doesn't work inside the DataTemplate element
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-11-17 06:36 UTC by Aboo
Modified: 2015-11-18 04:08 UTC (History)
5 users (show)

Tags: databinging, x:reference, binding, datatemplate
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Aboo 2015-11-17 06:36:14 UTC
I didn't know whic version to choose. I have the latest stable version for today but my version in VS2015 says 3.11.1594.0 .

I originally posted a comment here:

because I thought it's the same issue but since I was asked to open a new bug that's why I'm posting this here.
So please don't mark it as duplicate.

The issue is very simple to produce. Basically a binding with x:Reference works outside DataTemplate but the same clause doesn't work inside the DataTemplate element as shown in the code below.

The expected result is that all the rows in the list view have the same text as MyLabet text which is "Test" however they will be blank. The second label after MyLabel shows that the same binding pattern works in another position just fine.

This is very urgent for us so I appreciate some feedback.

<Label Text="Test" x:Name="MyLabel"></Label>
<Label Text="{Binding Source={x:Reference MyLabel}, Path=Text}"></Label>
<!--works here-->
<ListView ItemsSource="{Binding Options}" x:Name="OptionsListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell BindingContext="{x:Reference MyLabel}" Text="{Binding
Text}">></TextCell> <!--doesn't work here-->
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Comment 1 Aboo 2015-11-17 06:37:22 UTC
Sorry I forgot the link to my original post. Here you go:
https://bugzilla.xamarin.com/show_bug.cgi?id=30684
Comment 2 Stephane Delcroix 2015-11-18 03:37:06 UTC
> because I thought it's the same issue but since I was asked to open a new bug
> that's why I'm posting this here.
> So please don't mark it as duplicate.

Thanks for doing so :)
Comment 3 Stephane Delcroix 2015-11-18 04:08:15 UTC
> The second label after
> MyLabel shows that the same binding pattern works in another position just
> fine.

well, that's not the same binding pattern. in one case it is

    Text="{Binding Source={x:Reference MyLabel}, Path=Text}"

and in the other

    BindingContext="{x:Reference MyLabel}" Text="{Binding Text}"

Both works, and the x:Reference is works in both case, BUT in the case of the textcell, the BindingContext is overwritten by the ListView with the value of the ItemSource for that particular cell.

to get the desired behavior, do this:

    <TextCell Text="{Binding Source={x:Reference MyLabel}, Path=Text}"/>