TemplateBinding to ICommand property not work
See original GitHub issueDescribe the bug
TemplateBinding
doesn’t work for the ICommand
property with Button
inside template.
Steps to reproduce the bug
- 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); }
}
}
- 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>
- 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:
- Created 3 years ago
- Reactions:4
- Comments:10 (3 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 >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 >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 FreeTop 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
Top GitHub Comments
Same issue as in #7003 - still present in 1.2, very annoying bug. Also very surprised to learn that this hasn’t been fixed.
Any update on this?