Changing Button image when IsEnabled
Asked Answered
T

3

6

I want to change button image when button IsEnabled == False.

Below is my example, bindings are fine, when I change them for False/True it is still not working.

<Button x:Name="btnBackward" Grid.Column="0" Grid.Row="2" Command="{Binding UserWorkflowManager.NavigateBackward}" IsEnabled="{Binding UserWorkflowManager.NavigateBackwardEnable}" Grid.RowSpan="2">
    <Button.Template>
        <ControlTemplate>
            <Image Name="_image" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
                <Image.Style>
                    <Style TargetType="Image">
                        <Setter Property="Source" Value="/UserWorkflow.View;component/Images/LDC500_butX_PreviousPane_norm.bmp" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=btnBackward, Path=IsEnabled}" Value="False">
                                    <Setter Property="Source" Value="/UserWorkflow.View;component/Images/LDC500_butX_PreviousPane_dis.bmp"/>
                                </DataTrigger>
                            </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
        </ControlTemplate>
    </Button.Template>
</Button>
Touber answered 17/10, 2011 at 16:27 Comment(0)
L
7

Try the following

<Style.Triggers>
    <DataTrigger Binding="{Binding IsEnabled,RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
        <Setter Property="Source" Value="/UserWorkflow.View;component/Images/LDC500_butX_PreviousPane_dis.bmp"/>
    </DataTrigger>
</Style.Triggers>

Or add the trigger directly in the control template like so

            <ControlTemplate>
                <Image Name="_image" HorizontalAlignment="Center">
                 ..........
                </Image>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter TargetName="_image" Property="Source" Value="/UserWorkflow.View;component/Images/LDC500_butX_PreviousPane_dis.bmp" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
Lamoreaux answered 17/10, 2011 at 16:42 Comment(6)
I tested both the methods. Check your bindings.Lamoreaux
Bindings are fine, even when I set True/False it is not working.Krystin
Did you trace IsEnabled and verify that get is being called?Conciliar
I set it directly to TRUE/FALSE (omit binding)Krystin
@PawełSmejda post your code somewhere and post the link here. There must be some difference between my answer and your implementation.Lamoreaux
When I prepared project and extracted it to new it works. So thx I will find where the problem is.Krystin
R
2

Try this:

<Button x:Name="btnBackward" Grid.Column="0" Click="btnBackward_Click">
        <Button.Template>
            <ControlTemplate>
                <Image Name="_image" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
                    <Image.Style>
                        <Style TargetType="Image">
                            <Setter Property="Source" Value="/Images/0.png" />
                        </Style>
                    </Image.Style>
                </Image>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Source" Value="/Images/1.png" TargetName="_image"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

        </Button.Template>
    </Button>

You should specify "TargetName" property for a setter within "ControlTemplate.Triggers"

Regality answered 18/10, 2011 at 7:31 Comment(0)
C
0

The soution is is to use RelativeSource={RelativeSource Self}} in your DataTrigger.

<DataTrigger
  Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}}"
  Value="False">

Example

Add this style:

<Style x:Key="MainButtonStyle" TargetType="Button" BasedOn="{StaticResource ChromelessButtonStyle}">
    <Setter Property="Foreground" Value="Black" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsEnabled,  RelativeSource={RelativeSource Self}}" Value="True">
            <Setter Property="Background" Value="Green" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=IsEnabled,  RelativeSource={RelativeSource Self}}" Value="False">
            <Setter Property="Background" Value="Blue" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Then apply this style to the button:

<Button x:Name="ActionButton" Style="{StaticResource MainButtonStyle}" Command="{Binding MyCmd}" IsEnabled="{Binding ActionButtonEnabled}"/>
Chromatin answered 18/9, 2015 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.