Search code examples
windows-phone-7bindinglistboxsender

How to fill a rectangle with an image that is coming from a bound listbox on event fire in WP7


i want to create a greeting card maker app for WP7, when a user double taps an image from a listbox, I want that selected image to fill a rectangle on the same page. Im passing 50 images into the list box like this:

 public GCM()
        {
            InitializeComponent();
            var articles = new List<Article>();
            for (byte i = 1; i <= 20; i++)
            {
                Article article = new Article() { Name = "name"+i, ImagePath = "Assets/Images/Backgrounds/"+i+".jpg" };
                articles.Add(article);
            }
            listBox1.DataContext = articles;
        }

and its working fine, now heres an xml snippet:

<Rectangle Fill="#FFF4F4F5" Margin="28,24,30,148" Stroke="Black" Name="rect1" />
................(more code here).........................
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="10" >
                        <Image Name="bgs" Source="{Binding ImagePath}" Height="90" Width="90" DoubleTap="Load_BG" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

how can i fill the rectangle with the tapped image? this code (below) sets the string null everytime, no matter which image i select, although to my knowledge each has a different name and it should give different names for different images. I will use the name of the image to fill the rectangle. What am i doing wrong?

private void Load_BG(object sender, System.Windows.Input.GestureEventArgs e)
        {
            string abc = sender.GetType().Name;
        }

Please excuse me if the solution is obvious..this is my first app ever. Thank you!


Solution

  • so heres the solution, it works now :)

     private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var img = listBox1.SelectedItem as Article;
                ImageBrush imgBrush = new ImageBrush();
                imgBrush.ImageSource = new BitmapImage(new Uri(img.ImagePath, UriKind.RelativeOrAbsolute));
                rect1.Fill = imgBrush;           
            }