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.

TemplateBinding to ICommand property not work

See original GitHub issue

Describe the bug TemplateBinding doesn’t work for the ICommand property with Button inside template.

Steps to reproduce the bug

  1. Create CustomControl:
class MyControl : Control
{
    public static readonly DependencyProperty MyCommandProperty =
        DependencyProperty.Register("MyCommand", typeof(System.Windows.Input.ICommand), typeof(MyControl), new PropertyMetadata(null));

    public System.Windows.Input.ICommand MyCommand
    {
        get { return (System.Windows.Input.ICommand)GetValue(MyCommandProperty); }
        set { SetValue(MyCommandProperty, value); }
    }
}
  1. On root MainWindow:
<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="local:MyControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:MyControl">
                        <Grid>
                            <Button Command="{TemplateBinding MyCommand}"
                                    Content="RUN"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>

    <local:MyControl x:Name="MyControl" />
    <TextBlock x:Name="TextBlock" />
</StackPanel>
  1. On code behind MainWindow
public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();

        MyControl.MyCommand = new RelayCommand(DO); // Classic RelayCommand
    }

    private void DO(object obj)
    {
        TextBlock.Text = "PRESSED";
    }
}

Expected behavior After clicking on the button, the text must become “PRESSED”. But it’s not. Similar TemplateBindings to other properties work fine.

Workaround Change Command="{TemplateBinding MyCommand}" To Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=MyCommand}"

Version Info

NuGet package version: [Microsoft.ProjectReunion 0.5.0-prerelease] [Microsoft.ProjectReunion.Foundation 0.5.0-prerelease] [Microsoft.ProjectReunion.WinUI 0.5.0-prerelease]

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042) Yes
May 2020 Update (19041)
November 2019 Update (18363) Yes
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
andreaslennartzcommented, Jan 10, 2023

Same issue as in #7003 - still present in 1.2, very annoying bug. Also very surprised to learn that this hasn’t been fixed.

0reactions
Soap-141commented, May 30, 2023

Any update on this?

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 >
Xamarin forms templatebinding. I cannot update ...
Forms Compiled Bindings does not work on DataTemplate. forms version is 4. at Xamarin. Forms cell type. How to pass Binding Property via...
Read more >
Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4
... same purpose—extracting data from the properties of your custom control—the lighter-weight TemplateBinding is always appropriate. It won't work if you ...
Read more >
Professional Silverlight 2 for ASP.NET Developers
Foreground=”{TemplateBinding Foreground}” HorizontalAlignment=”Left” ... Note how you can extend the property binding to the font objects, which is not as ...
Read more >
MCTS Microsoft Silverlight 4 Development (70-506) ...
... CommandParameter property / Building your first ICommand Command property ... /Creating control templates TemplateBinding / TemplateBinding content, ...
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