Saturday 19 November 2011

How to add image button Or How to add Image to Button in Silverlight?

How to add image button Or How to add Image to Button in Silverlight?

Ans=>
There is no need to add image tag or image button in the xaml file only add normal button to the xaml file like and add the image to the content property of the button it is same like Text property in .Net

   <Button.Content>
     <Image Height="20" HorizontalAlignment="Left" Name="imageTest" Stretch="Fill" Width="47" VerticalAlignment="Top" Source="D:\Projects\SilverlightApplication\Images\ABC.png" />
   </Button.Content>
</Button>

You can also add at runtime:

Add normal button to the page like

<Button x:Name="btnSave" x:Uid="1" Width="28" Height="28"></Button>

Then go to the code behind then depend on your condition you can add the following code into the various events like page load on click here I am adding image to the button in page load

  private void Page_Loaded(object sender, RoutedEventArgs e)
        {

   Uri urileft = new Uri("/SilverlightApplication/Images/ABC.png", UriKind.Relative);
                BitmapImage imgSource = new BitmapImage(urileft);
                Image image = new Image();
                image.Source = imgSource;

                btnSave.Content = image;
}

in the above code first four line is used to declare image object and to set image path then last two line is used where to add the image or we can say on which button u want to add image.
Like above code btnSave is normal button and content is property if u add
                btnSave.Content = image; //then image will set into the background of button.



<Button Height="24" Width="49" HorizontalAlignment="Left" Name=" btnSave " VerticalAlignment="Top">

No comments:

Post a Comment