How to get the URL of the image of the particular label element in Silverlight?

advertisements

I am binding the items as follows:

    <ScrollViewer>
       <ItemsControl x:Name="UserList">
          <ItemsControl.ItemsPanel>
             <ItemsPanelTemplate>
                 <StackPanel Orientation="Horizontal" />
             </ItemsPanelTemplate>
          </ItemsControl.ItemsPanel>
          <ItemsControl.ItemTemplate>
             <DataTemplate>
                <Image Source="{Binding imageurl}"
                       Tag="{Binding Path=id}" Width="164" Height="150"
                       Margin="4" Stretch="Fill"></Image>

             </DataTemplate>
          </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>

Code.cs:

    List.Add(new StackImages { id = "1", Name="test",type="test",price ="testprice" ,imageurl = new Uri(this.BaseUri, @"Assets/acservice.png") });
    List.Add(new StackImages { id = "2", imageurl = new Uri(this.BaseUri, @"Assets/brakes.png") });
    List.Add(new StackImages { id = "3", imageurl = new Uri(this.BaseUri, @"Assets/carwash.png") });
    List.Add(new StackImages { id = "4", imageurl = new Uri(this.BaseUri, @"Assets/oilchange.png") });
    List.Add(new StackImages { id = "5", imageurl = new Uri(this.BaseUri, @"Assets/transmission.png") });
    UserList.ItemsSource= List;

Please tell me how to get particulat imageurl of the particular tag value when tap on it?


Based on the answer given to your previous question you have two options. Either you access the Source property of the Image control and cast it to BitmapImage (that's what WPF creates by automatic type conversion from Uri to ImageSource):

private void MyTapHandler(Object sender, EventArgs e)
{
    Image image = (Image)sender;
    BitmapImage bitmap = (BitmapImage)image.Source;
    Uri uri = bitmap.UriSource;
}

Or you access the StackImage object in the Image's DataContext:

private void MyTapHandler(Object sender, EventArgs e)
{
    Image image = (Image)sender;
    StackImage stackImage = (StackImage)image.DataContext;
    Uri uri = stackImage.imageurl;
}