Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

UserControl - TwoWay-Binding in ControlTemplate

$
0
0

Hi,
I have a UserControl similar to this:

public class MyUserControl : UserControl
{
 public static readonly DependencyProperty ButtonClickProperty=DependencyProperty.Register("ButtonClick", typeof(ICommand), typeof(MyUserControl), new UIPropertyMetadata(null));

 public ICommand ButtonClick
 {
   get { return (ICommand)GetValue(ButtonClickProperty); }
   set { SetValue(ButtonClickProperty, value); }
 }
}

Style:

<Style TargetType="{x:Type controls:MyUserControl}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type controls:MyUserControl}"><Border><Grid><RadioButton x:Name="RadioButton" Command="{Binding ButtonClick, RelativeSource={RelativeSource TemplatedParent}}" IsEnabled="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsEnabled, Mode=TwoWay}" /></Grid></Border><ControlTemplate.Triggers><Trigger Property="IsEnabled" Value="False"><Setter TargetName="RadioButton" Property="Cursor" Value="Arrow" /></Trigger><Trigger Property="IsEnabled" Value="True"><Setter TargetName="RadioButton" Property="Cursor" Value="Hand" /></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>

As expected, the command on the RadioButton works fine and sets RadioButton.IsEnabled to false when CanExecute also returns false. My understanding was that unlike TemplateBinding, using Binding with RelativeSource TemplatedParent works fine as a TwoWay-Binding, and that IsEnabled as set by the ButtonClick-Command should therefore propagate up the tree to set IsEnabled on MyUserControl as well. However, that doesn't happen - MyUserControl.IsEnabled always returns true, and the trigger for IsEnabled = false never fires. What am I missing?



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>