uno icon indicating copy to clipboard operation
uno copied to clipboard

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

Open takla21 opened this issue 1 year ago • 1 comments

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

takla21 avatar Apr 07 '23 02:04 takla21