Enable child control if parent is disabled

I have a button containing a hyperlink, for example:

<Button IsEnabled="False"> <Hyperlink IsEnabled="True">Testing</Hyperlink> </Button> 

I need to enable the hyperlink, however the button that needs to be disabled. How can I achieve this?

The above simply causes both controls to be disabled.

+6
source share
1 answer

The Hyperlink control is weird with the IsEnabled property. In addition to what you mentioned, namely, full inheritance from the parent, there is another similar one.

Hyperlink for a specific control that has been disabled ( IsEnabled="False" ), setting ( IsEnabled="True" ) will not update the Hyperlink property. Solution - use relative source for Hyperlink (more info ).

To solve your question, I decided that this is not a standard solution. So I created a Class with my own dependency properties. It has the property MyIsEnabled and MyStyle . As you can guess from the name, the first sets its IsEnabled property and MyStyle needs to specify the button style, imitating the behavior of IsEnabled="False" .

SimulateDisable Style

 <Style x:Key="SimulateDisable" TargetType="{x:Type Button}"> <Setter Property="Opacity" Value="0.5" /> <Setter Property="Background" Value="Gainsboro" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border CornerRadius="4" BorderThickness="1" BorderBrush="DarkBlue" SnapsToDevicePixels="True"> <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> 

Define Button with your properties:

 <Button Name="MyButton" local:MyClass.MyIsEnabled="False" local:MyClass.MyStyle="{StaticResource SimulateDisable}" Width="100" Height="30" Click="Button_Click"> <Hyperlink IsEnabled="True" Click="Hyperlink_Click">Testing</Hyperlink> </Button> 

Listing of MyClass

 public class MyClass : DependencyObject { public static readonly DependencyProperty MyIsEnabledProperty; public static readonly DependencyProperty MyStyleProperty; #region MyIsEnabled public static void SetMyIsEnabled(DependencyObject DepObject, bool value) { DepObject.SetValue(MyIsEnabledProperty, value); } public static bool GetMyIsEnabled(DependencyObject DepObject) { return (bool)DepObject.GetValue(MyIsEnabledProperty); } #endregion MyIsEnabled #region MyStyle public static void SetMyStyle(DependencyObject DepObject, Style value) { DepObject.SetValue(MyStyleProperty, value); } public static Style GetMyStyle(DependencyObject DepObject) { return (Style)DepObject.GetValue(MyStyleProperty); } #endregion MyStyle static MyClass() { MyIsEnabledProperty = DependencyProperty.RegisterAttached("MyIsEnabled", typeof(bool), typeof(MyClass), new UIPropertyMetadata(false, OnPropertyChanged)); MyStyleProperty = DependencyProperty.RegisterAttached("MyStyle", typeof(Style), typeof(MyClass), new UIPropertyMetadata(OnPropertyChanged)); } private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { Button MyButton = sender as Button; bool MyBool = GetMyIsEnabled(MyButton); if (MyBool == false) { MyButton.Style = MyClass.GetMyStyle(MyButton); } } } 

Plus for a Hyperlink event indicating e.Handled = true so that the event does not follow.

 private void Hyperlink_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hyperlink Click!"); e.Handled = true; } private void Button_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Button Click! Don't show it's!"); } 

Output

enter image description here

PS Sorry for the late reply :).

0
source

All Articles