<phone:LongListSelector x:Name="MainLongListSelector" DataContext="{Binding listData}" IsGroupingEnabled="False" SelectionChanged="MainLongListSelector_SelectionChanged_1">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="PlayPause" Source="{Binding ImgUrl}" Height="100" Stretch="Fill"/>
<StackPanel>
<TextBlock Text="{Binding ImgText}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
XAML 的背后:
ObservableCollection<LongListData> listData = new ObservableCollection<LongListData>();
listData.Add(new LongListData() { ImgText = "Image1", ImgUrl = new Uri("Images/1.jpg", UriKind.Relative) });
listData.Add(new LongListData() { ImgText = "Image2", ImgUrl = new Uri("Images/2.jpg", UriKind.Relative) });
listData.Add(new LongListData() { ImgText = "Image3", ImgUrl = new Uri("Images/3.jpg", UriKind.Relative) });
listData.Add(new LongListData() { ImgText = "Image4", ImgUrl = new Uri("Images/4.jpg", UriKind.Relative) });
MainLongListSelector.ItemsSource = listData;
在 SelectionChanged 方法中
private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var res = (sender as LongListSelector).SelectedItem as LongListData;
int index = listData.IndexOf(res);
//mark 1
var newData = new LongListData() { ImgText = res.ImgText, ImgUrl = new Uri("Images/2.jpg", UriKind.Relative) };
listData.RemoveAt(index);
listData.Insert(index, newData);
}
在 C# 代码中定义一个类
public class LongListData
{
public Uri ImgUrl { get; set; }
public string ImgText { get; set; }
}
您可以在代码中的“mark1”处随机或根据需要使用图像。