Skip to content

WinUI - TemplateBinding not working for Commands #7003

@DRAirey1

Description

@DRAirey1

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions