[IronPython] Silverlight code need help (Repost)

Jimmy Schementi Jimmy.Schementi at microsoft.com
Mon Jan 11 23:32:55 CET 2010


And to answer your initial question about images in buttons, here's how to do it:

from System.Windows.Controls import Button, Image
from System.Windows.Media.Imaging import BitmapImage

btn = Button(
    Width = 25, 
    Height = 20,
    Content = Image(
        Source = BitmapImage(Uri("images/stop.jpg", UriKind.Relative))
    )
)

This assumes that "images/stop.jpg" is relative to the XAP file of your application. To add an image to a System.Windows.Controls.Button, you must use System.Windows.Controls.Image. The Image control then lets you set its Source, which is where System.Windows.Media.Imaging.BitmapImage comes into play. Confusing, I know =(

This syntax uses IronPython's ability to use Python's named parameters to set properties; it makes working with Silverlight and WPF objects a lot less painful. Not using named parameters would look like this:

btn = Button()
btn.Width = 25
btn.Height = 20
img = Image()
img.Source = BitmapImage(Uri("images/stop.jpg", UriKind.Relative))
btn.Content = img

Oy =P

Let me know if you have any other Silverlight questions,
~Jimmy



More information about the Ironpython-users mailing list