question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WinUI - TemplateBinding not working for Commands

See original GitHub issue

Describe the bug

TemplateBinding to an ICommand property doesn’t work in WinUI. However, explicitly binding using the RelativeSource does. BgLabelControlApp.zip This doesn’t work:

<ResourceDictionary xmlns:local="using:ClassLibrary1"
					xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
	<Style TargetType="local:BgLabelControl">
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="local:BgLabelControl">
					<Grid Background="{TemplateBinding Background}"
						  Height="100"
						  Width="100">
						<Button Content="{TemplateBinding Label}"
								HorizontalAlignment="Center"
                                Command="{TemplateBinding GoBack}"
					</Grid>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</ResourceDictionary>

This works:

<ResourceDictionary xmlns:local="using:ClassLibrary1"
					xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
	<Style TargetType="local:BgLabelControl">
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="local:BgLabelControl">
					<Grid Background="{TemplateBinding Background}"
						  Height="100"
						  Width="100">
						<Button Content="{TemplateBinding Label}"
								HorizontalAlignment="Center"
                                Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=GoBack, Mode=OneWay}"
								VerticalAlignment="Center"/>
					</Grid>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</ResourceDictionary>

Steps to reproduce the bug

  1. Expand the attached zip file.
  2. Run the application. The second button doesn’t work
  3. Replace the TemplateBinding with “{Binding RelativeSource={RelativeSource TemplatedParent}, Path=GoBack, Mode=OneWay}”
  4. Run the application. The second button works.

Expected behavior

One expects TemplateBinding to bind to the templated parent.

Screenshots

No response

NuGet package version

No response

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

Windows 11 (21H2): Build 22000

Additional context

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
Soap-141commented, May 30, 2023

Any update on this?

2reactions
andreaslennartzcommented, Jan 10, 2023

Just stumbled across this exact same issue when creating a very simple CustomControl.

Can confirm that this is still a thing in 1. Template binding works properly for all other properties, but for some reason it doesn’t work with commands. The mentioned workaround (Replace {TemplateBinding GoBack} with {Binding RelativeSource={RelativeSource TemplatedParent}, Path=GoBack, Mode=OneWay}) works.

The solution is simple, but took me a while to find this - and to me this a very annoying bug. Should have a higher priority. Hope this can get fixed soon!

Read more comments on GitHub >

github_iconTop Results From Across the Web

TemplateBinding not working with command WPF
When I bind the command of the button to the 'AddCommand' through TemplateBinding it won't work. I have created a custom control as...
Read more >
Simple WinUI problem : r/dotnet
My apologies. Here we have a button that is defined on a templated control: <Button Content="Click Me" Command="{Binding RelativeSource={ ...
Read more >
TemplateBinding markup extension - UWP applications
Attempting to use a TemplateBinding outside of a ControlTemplate definition in XAML will result in a parser error. You can use TemplateBinding ...
Read more >
NET MAUI TreeView Control Template - Documentation
The Telerik UI for .NET MAUI TreeView provides a ControlTemplate property, which defines the visual appearance of the control. To customize the looks...
Read more >
PanelControlBox Button Commands
Hi! I'm trying to make the buttons in the panel control box larger. I've been trying to follow the solutions in these support...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found