0

我将项目绑定如下:

<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>

代码.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;

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

4

1 回答 1

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

} 
于 2013-07-31T11:34:17.357 回答