Sharker Khaleed Mahmud Silverlight Tips & Tricks

January 20, 2010

Tricks and Tips 14 : Silverlight image binding at runtime

MainPage.xaml

<UserControl xmlns:my=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”  x:Class=”Tips.MainPage”
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
    xmlns:d=”http://schemas.microsoft.com/expression/blend/2008
    xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006
    mc:Ignorable=”d”
    d:DesignHeight=”300″ d:DesignWidth=”400″>
    <Grid x:Name=”LayoutRoot”>
        <ListBox x:Name=”imgList”>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Left”>
                        <Image Source=”{Binding Url}” Width=”150″ />
                        <TextBlock Text=”{Binding Title}” Width=”100″ Height=”30″ VerticalAlignment=”Center” />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

MainPage.xaml.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;

namespace Tips
{
    public class ListItem
    {
        public string Title { get; set; }
        public ImageSource Url { get; set; }
    }
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            List<ListItem> items = new List<ListItem>();
            items.Add(new ListItem
            {
                Title = “7698_3d_world’s_Residents”,
                Url = new BitmapImage(
                    new Uri(“images/7698_3d_world’s_Residents.jpg”, UriKind.Relative))
            });
            items.Add(new ListItem
            {
                Title = “8198_digital_Hillary_picture_2″,
                Url = new BitmapImage(
                    new Uri(“images/8198_digital_Hillary_picture_2.jpg”, UriKind.Relative))
            });
            items.Add(new ListItem
            {
                Title = “8823_Megan_Fox.jpg”,
                Url = new BitmapImage(
                    new Uri(“images/8823_Megan_Fox.jpg”, UriKind.Relative))
            });
            items.Add(new ListItem
            {
                Title = “9484_Jennifer_Hewitt_Wallpaper.jpg”,
                Url = new BitmapImage(
                    new Uri(“images/9484_Jennifer_Hewitt_Wallpaper.jpg”, UriKind.Relative))
            });
            imgList.ItemsSource = items;
        }
    }
}
Sharker Khaleed Mahmud
Software Developer
(MCP,MCTS,MCPD[web])

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.