Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable a child control when the parent is disabled

I have a Button containing a Hyperlink, like so:

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

I need the Hyperlink to be enabled, however the Button to be disabled. How can I achieve this?

The above simply results in both controls being disabled.

like image 872
Justin Avatar asked Jan 29 '13 13:01

Justin


Video Answer


1 Answers

I solved this problem by creating a simple wrapper element that breaks the IsEnabled inheritance chain from the parent.

The framework's default coerce callback checks the parent IsEnabled value and inherits it. This control sets a new coerce callback that just returns the value directly without checking inheritance.

public class ResetIsEnabled : ContentControl
{
    static ResetIsEnabled()
    {
        IsEnabledProperty.OverrideMetadata(
            typeof(ResetIsEnabled),
            new UIPropertyMetadata(
                defaultValue: true,
                propertyChangedCallback: (_, __) => { },
                coerceValueCallback: (_, x) => x));
    }
}

In the example from the question it would be used like this:

<Button IsEnabled="False">
  <ResetIsEnabled>
    <!-- Child elements within ResetIsEnabled have IsEnabled set to true (the default value) -->
    <Hyperlink>Testing</Hyperlink>
  </ResetIsEnabled>
</Button>
like image 153
mark.monteiro Avatar answered Sep 23 '22 00:09

mark.monteiro