uno
uno copied to clipboard
`RelativeSource` binding is not supported inside `ListView.ItemTemplate`
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'}" />
Expected behavior
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'}" />
- Download this sample RelativeSourceBinding.zip
- Deploy on any uno platform
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