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>

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;

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

4

2 回答 2

1

根据对上一个问题的回答,您有两个选择。您可以访问SourceImage 控件的属性并将其转换为BitmapImage(这是 WPF 通过从Urito的自动类型转换创建的ImageSource):

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

或者您访问StackImage图像中的对象DataContext

private void MyTapHandler(Object sender, EventArgs e)
{
    Image image = (Image)sender;
    StackImage stackImage = (StackImage)image.DataContext;
    Uri uri = stackImage.imageurl;
}
于 2013-08-01T10:56:55.573 回答
0

What you may want to do is to select an item from a list. In order to support selection, you have to replace the ItemsControl by a ListBox.

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

Now you may attach a handler to the SelectionChanged event and you may always access the selected item by the SelectedItem or SelectedIndex properties.

private void UserList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var selectedImage = UserList.SelectedItem as StackImage;
    if (selectedImage != null)
    {
        var uri = selectedImage.imageurl;
    }
}
于 2013-08-01T11:33:12.320 回答