3

I want change the content of the button to another image when it is pressed but nothing happens.

<Grid>
    <Button Height="120" Width="75">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Image Source="no-press.png"/>
                <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Content">
                                <Setter.Value>
                                    <DataTemplate>
                                        <Image Source="press.png"/>
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>      
    </Button>        
</Grid>

thank you :)

Contango
  • 76,540
  • 58
  • 260
  • 305
Yair H
  • 67
  • 1
  • 9
  • 1
    I answered a similar question recently whereby the user wanted to change the content of a button based on a view model or backing property. There is a button click handler which could set this property in your case > http://stackoverflow.com/questions/30685101/contentcontrol-switch-between-two-buttons/30686307#30686307 – James Harcourt Jun 07 '15 at 22:23

1 Answers1

6

Here you go:

<Grid>
    <Button Height="120" Width="75">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="Transparent" BorderThickness="1" BorderBrush="Black">
                                <ContentPresenter Name="content"/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="Button.IsPressed" Value="True">
                                    <Setter TargetName="content" Property="ContentPresenter.Content">
                                        <Setter.Value>
                                            <Image Source="pressed.png"/>
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
        <Image Source="no-pressed.png"/>
    </Button>
</Grid>
d.moncada
  • 16,900
  • 5
  • 53
  • 82