Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RelativeSource binding is not supported inside ListView.ItemTemplate #11925

Open
takla21 opened this issue Apr 7, 2023 · 1 comment
Open
Labels
difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/bug Something isn't working project/binding 🪢 Categorizes an issue or PR as relevant to the binding engine project/items 🧾 Categorizes an issue or PR as relevant to items (ItemsControl, ItemsRepeater, ...) triage/potentially-fixed Categorizes an issue as potentially fixed by some unlinked PR, fix needs to be verified

Comments

@takla21
Copy link
Contributor

takla21 commented Apr 7, 2023

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 takla21 added difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/bug Something isn't working triage/untriaged Indicates an issue requires triaging or verification labels Apr 7, 2023
@Xiaoy312 Xiaoy312 self-assigned this Apr 14, 2023
@MartinZikmund MartinZikmund changed the title RelativeSource binding is not supported inside ListView ItemTemplate on uno RelativeSource binding is not supported inside ListView.ItemTemplate Oct 25, 2023
@MartinZikmund MartinZikmund added project/binding 🪢 Categorizes an issue or PR as relevant to the binding engine project/items 🧾 Categorizes an issue or PR as relevant to items (ItemsControl, ItemsRepeater, ...) difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI and removed triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Oct 25, 2023
@MartinZikmund
Copy link
Member

This may be fixed now with #17645

@MartinZikmund MartinZikmund added the triage/potentially-fixed Categorizes an issue as potentially fixed by some unlinked PR, fix needs to be verified label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty/medium 🤔 Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI kind/bug Something isn't working project/binding 🪢 Categorizes an issue or PR as relevant to the binding engine project/items 🧾 Categorizes an issue or PR as relevant to items (ItemsControl, ItemsRepeater, ...) triage/potentially-fixed Categorizes an issue as potentially fixed by some unlinked PR, fix needs to be verified
Projects
None yet
Development

No branches or pull requests

3 participants