unoplatform / uno

Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
https://platform.uno
Apache License 2.0
8.96k stars 729 forks source link

`RelativeSource` binding is not supported inside `ListView.ItemTemplate` #11925

Open takla21 opened 1 year ago

takla21 commented 1 year ago

Current behavior

When trying to bind to the datacontext with RelatedSource, it will display the fallback value.

<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}, FallbackValue='RelativeSource binding failed'}" />

image

Expected behavior

image

In UWP, we can use RelativeSource inside our bindings to get to the DataContext. It's quite useful especially when we need to use a converter.

How to reproduce it (as minimally and precisely as possible)

<ListView ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="Parent" DataContext="{Binding}">
                <TextBlock Text="{Binding}" />
                <TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}, FallbackValue='RelativeSource binding failed'}" />
                <TextBlock Text="{Binding ElementName=Parent, Path=DataContext, FallbackValue='Workaround binding failed'}" />

Workaround

Store the datacontext in a parent xaml element and bind from it.

<StackPanel x:Name="Parent"
           Background="Red"
           DataContext="{Binding}">

    <!-- Workaround text -->
    <TextBlock Text="{Binding ElementName=Parent, Path=DataContext, FallbackValue='Workaround binding failed'}" />
</StackPanel>

Works on UWP/WinUI

Yes

Environment

Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia

NuGet package version(s)

4.9.0-dev.439

Affected platforms

Android, iOS

IDE

Visual Studio 2022

IDE version

17.4.3

Relevant plugins

No response

Anything else we need to know?

No response

MartinZikmund commented 1 month ago

This may be fixed now with #17645