WinUI - TemplateBinding not working for Commands
See original GitHub issueDescribe 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
- Expand the attached zip file.
- Run the application. The second button doesn’t work
- Replace the TemplateBinding with “{Binding RelativeSource={RelativeSource TemplatedParent}, Path=GoBack, Mode=OneWay}”
- 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:
- Created a year ago
- Reactions:3
- Comments:10 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Any update on this?
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!