We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

"ImageButton" Change backround image on pressed

Hi!

I would like the same behavior as an ImageButton, the button should just show an image and when pressed I would like to change the image to another one.. so I tried the following code, but it does not work. Any tips how to make this work?


<syncfusion:SfButton x:Name="iconButton"
                        IsCheckable="False"
                        BorderWidth="0"
                        BackgroundColor="Transparent" 
                        PressedBackgroundColor="Transparent">
    <syncfusion:SfButton.Content>
        <Image Source="Test1.png" 
                HorizontalOptions="Center" 
                VerticalOptions="Center"/>
    syncfusion:SfButton.Content>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Pressed" >
                <VisualState.Setters>
                    <Setter Property="Content">
                        <Image Source="Test2.png" 
                                HorizontalOptions="Center" 
                                VerticalOptions="Center"/>
                    Setter>
                VisualState.Setters>
            VisualState>
            <VisualState x:Name="Normal">
                <VisualState.Setters>
                    <Setter Property="Content">
                        <Image Source="Test1.png" 
                                HorizontalOptions="Center" 
                                VerticalOptions="Center"/>
                    Setter>
                VisualState.Setters>
            VisualState>
        VisualStateGroup>
    VisualStateManager.VisualStateGroups>
syncfusion:SfButton>

3 Replies

GR Geetha Rajendran Syncfusion Team November 15, 2019 01:06 PM UTC

Hi Ulf, 
  
Greetings from Syncfusion. 
  
We would like to let you know that your requirement “Having different image on press and normal state on SfButton” has been achieved by setting the ImageSource property of SfButton. By default, we have property to show the Image with help of ShowIcon and ImageSource properties as per in below code example 
  
Code Example [XAML]: 
  
<buttons:SfButton ShowIcon="True" 
                ImageSource="Normal.png"> 
    <VisualStateManager.VisualStateGroups> 
        <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Pressed" > 
                <VisualState.Setters> 
                    <Setter Property="ImageSource" Value="Pressed.png"> 
                    </Setter> 
                </VisualState.Setters> 
            </VisualState> 
            <VisualState x:Name="Normal"> 
                <VisualState.Setters> 
                    <Setter Property="ImageSource" Value="Normal.png"> 
                    </Setter> 
                </VisualState.Setters> 
            </VisualState> 
        </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 
</buttons:SfButton> 
 
Please download the prepared sample in below link 
  
  
Please let us know if you have any other concern. 
 
Thanks, 
Geetha R 



UJ Ulf J November 18, 2019 09:21 AM UTC

Hi!

Thank for the fast reply!

The example works but it displays an icon inside the button, I would like the image to become the complete button,

This shows the the result: Big image is from the example I posted (where pressed and normal is not working, but the image is displayed correct)  the smaller image is from your example (it works when it comes to changed image on press and release, but I would like the image look like the left one), the image must become the complete button as an ImageButton works. How can I do that?



Thanks
Ulf


HM Hemalatha Marikumar Syncfusion Team November 19, 2019 01:13 PM UTC

Hi Ulf, 
 
Thanks for your update. 
 
If your requirement to fill only the provided image with overall button then, it has been achieved with the help of setting BackgroundImage property itself as per in below code snippet 
  
Code snippet [XAML] 
<buttons:SfButton BackgroundImage="Normal.png"                          
                   HorizontalOptions="Center" 
                   HeightRequest="40" 
                   WidthRequest="40" 
                   VerticalOptions="Center"> 
     <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
             <VisualState x:Name="Pressed" > 
                 <VisualState.Setters> 
                     <Setter Property="BackgroundImage" Value="Pressed.png"> 
                     </Setter> 
                 </VisualState.Setters> 
             </VisualState> 
             <VisualState x:Name="Normal"> 
                 <VisualState.Setters> 
                     <Setter Property="BackgroundImage" Value="Normal.png"> 
                     </Setter> 
                 </VisualState.Setters> 
             </VisualState> 
         </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 
</buttons:SfButton> 
  
Please get back to us if you need any further assistance on this. 
  
Regards, 
Hemalatha M. 
 


Loader.
Live Chat Icon For mobile
Up arrow icon