Search code examples
winrt-xamlwindows-10windows-10-mobile

Change style of a Button when its Disabled ( IsEnabled=False )


I have a UWP Question. How can I change style of a Button when it’s Disabled (IsEnabled=False)?


Solution

  • Here's how you do it.

    <StackPanel>
    
        <Button x:Name="button" Content="Click Me" 
                IsEnabled="{Binding IsChecked, ElementName=checkBox}">
            <Interactivity:Interaction.Behaviors>
    
                <Core:DataTriggerBehavior Binding="{Binding IsEnabled,
                    ElementName=button, Mode=OneWay}" Value="True">
                    <Core:ChangePropertyAction PropertyName="Opacity" Value="1"/>
                </Core:DataTriggerBehavior>
    
                <Core:DataTriggerBehavior Binding="{Binding IsEnabled, 
                    ElementName=button, Mode=OneWay}" Value="False">
                    <Core:ChangePropertyAction PropertyName="Opacity" Value=".5"/>
                </Core:DataTriggerBehavior>
    
            </Interactivity:Interaction.Behaviors>
        </Button>
    
        <CheckBox x:Name="checkBox" IsChecked="True" />
    
    </StackPanel>
    

    Don't forget you need to reference the Behaviors SDK.

    Best of luck!