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

WinUI - TemplateBinding not working for Commands #7003

Open
2 tasks
DRAirey1 opened this issue Apr 20, 2022 · 10 comments
Open
2 tasks

WinUI - TemplateBinding not working for Commands #7003

DRAirey1 opened this issue Apr 20, 2022 · 10 comments
Labels
area-Binding team-Markup Issue for the Markup team

Comments

@DRAirey1
Copy link

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

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Apr 20, 2022
@MikeHillberg
Copy link
Contributor

{TemplateBinding} only supports binding to a property that's backed by a DependencyProperty, which could be causing this. {Binding} uses reflection and generated code-behind to bind to any WinRT or C# property.

@DRAirey1
Copy link
Author

Did you look at the code? What makes you think it's not binding to a DependencyProperty?

@kmgallahan
Copy link
Contributor

Dupe of #4546 ?

@DRAirey1
Copy link
Author

Yeah, it's likely. The notes say that someone's having trouble with the repro, so the ZIP file here is an easy way to validate that you have a real bug.

@MikeHillberg
Copy link
Contributor

Did you look at the code? What makes you think it's not binding to a DependencyProperty?

Sorry, I was looking at the code in the Issue and didn't notice the zip file link. Thanks for the repro.

@kmgallahan
Copy link
Contributor

kmgallahan commented Apr 20, 2022

May also be related to #2508 in that x:Bind behevior in templates & resource dictionaries is finicky, failing there when:

x:Bind to a property outside the data template works fine ✅
x:Bind to that same property but with function binding fails to build ❌
x:Bind to that same property from a visual state setter crashes at runtime ❌
x:Bind to that same property with a converter crashes at runtime ❌

@DRAirey1
Copy link
Author

DRAirey1 commented Apr 20, 2022

May also be related to #2508 in that x:Bind behavior in templates & resource dictionaries is finicky, failing there when:

No. x:Bind is done in code behind. This is not code-behind related code. Definitely related to #4546

@ojhad ojhad added area-Binding team-Markup Issue for the Markup team labels Apr 25, 2022
@Panda-Sharp
Copy link

Just started to play with WinUI, I started with 1.2, and I just hit this issue #4546

@bpulliam bpulliam removed the needs-triage Issue needs to be triaged by the area owners label Dec 6, 2022
@andreaslennartz
Copy link

andreaslennartz commented 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!

@Soap-141
Copy link

Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

8 participants