tags:

views:

642

answers:

3

I want to change the image of the button in the code below based on its state i.e. use different image for enable and disable state.

<Button CommandParameter="Open" >
    <StackPanel Orientation="Horizontal" >
        <Image Source="../icons/big/open.png" Width="20" Height="20"></Image>
    </StackPanel>
</Button>

Thank you.

A: 

Try using Style.Triggers see post wpf-style-trigger

Nils
A: 

Basically, create a style for the button which makes it display an image object in its content property, and then have a trigger which checks the buttons enabled state, and when it is TRUE have it be one image, and at all other times be the other image.

Stephen Wrighton
+2  A: 

You can use a style with triggers like this:

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <StackPanel Orientation="Horizontal" >
                    <Image Name="PART_Image" Source="path to normal image" />
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Source" Value="path to mouse over image" TargetName="PART_Image"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Source" Value="path to pressed image" TargetName="PART_Image"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Source" Value="path to disabled image" TargetName="PART_Image"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
John Myczek
Is there no way to use some kind of graying-out filter (and others) to generate the images on the fly? Do we have to have essentially hard-coded images of the different states?
Mal Ross
Yes, you could use the same logic as described above and change the Opacity property of the image. You could also do things like adding a Border to the template and show/hide it when the mouse is over the image, or change the background color ...
John Myczek