<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#代碼定義class
public class LongListData
{
public Uri ImgUrl { get; set; }
public string ImgText { get; set; }
}
您可以在代碼中'mark1'隨機或根據您的需要使用圖像。
爲什麼不讓項目列出每個對象都有不同圖像的列表? –
這是我從服務器獲得的曲目列表,並且圖像「播放\暫停」對於每個對象都必須相同,並且當用戶點擊時,我需要更改圖像以「暫停」選定的對象。圖像應該在VM實體中?如何做到這一點你說什麼? –