Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the image when the button is disabled?

Tags:

button

image

wpf

I'm trying to show a different image when the button is disabled; I thought it would be easy with triggers.

However, I have not been able to get the image source to switch to the disabled image when the button is disabled. I've tried setting triggers on both the image and button. What is wrong with what I have below? How can I change the image source when the button is enabled/disabled?

<Button
         x:Name="btnName"
         Command="{Binding Path=Operation}"
         CommandParameter="{x:Static vm:Ops.OpA}">
            <Button.Content>
                <StackPanel>
                    <Image
                  Width="24"
                  Height="24"             
                  RenderOptions.BitmapScalingMode="NearestNeighbor"
                  SnapsToDevicePixels="True"
                  Source="/MyAssembly;component/images/enabled.png">
                        <Image.Style>
                            <Style>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=btnName, Path=Button.IsEnabled}" Value="False">
                                        <Setter Property="Image.Source" Value="/MyAssembly;component/images/disabled.png" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </StackPanel>
            </Button.Content>
        </Button>
like image 427
Rob Buhler Avatar asked May 14 '10 01:05

Rob Buhler


People also ask

What is the color of disabled button?

Gray is often used to communicate a low priority button (e.g., cancel buttons). When they see a gray button, they won't know for sure if it's disabled unless they click it.


1 Answers

Yeah this one pops up quite a bit.

Any property that's explicitly set in the object's declaration can't be changed in a style. So because you've set the image's Source property in the declaration of the image, the style's Setter won't touch it.

Try this instead:

<Image
    Width="24"  
    Height="24"               
    RenderOptions.BitmapScalingMode="NearestNeighbor"  
    SnapsToDevicePixels="True"
    >
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Source"
                    Value="/MyAssembly;component/images/enabled.png" />
            <Style.Triggers>
                ... your trigger and setter ...
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>
like image 176
Matt Hamilton Avatar answered Oct 24 '22 20:10

Matt Hamilton