如何在Silverlight下面的代码行中获取图像的标记值?

时间:2013-07-31 11:27:50

标签: silverlight

我按以下方式绑定项目:

<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", 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;

请点击它时告诉我如何获得特定图像标记值?

1 个答案:

答案 0 :(得分:1)

为您的图片添加Tap处理程序。

...
<DataTemplate>
  <Image Source="{Binding imageurl}" Tap="MyTapHandler"
         Tag="{Binding Path=id}" Width="164" Height="150" 
         Margin="4" Stretch="Fill"></Image>
</DataTemplate>

在代码背后:

private void MyTapHandler(Object sender, EventArgs e)
{
    Image sourceImage = sender as Image;
    string id = sourceImage.Tag as string;

    //do stuff with id

}