2013-06-27 60 views
1

我的Windows Phone應用程序中有一個LongListSelector。 LLS爲每個項目都有一個Image和TextBlock。如何在用戶更改選擇時更改圖像?我應該在我的SelectionChanged方法中寫什麼?如何更改LongListSelector SelectionChanged方法中的圖像?

<phone:LongListSelector x:Name="MainLongListSelector" ItemsSource="{Binding Items}" SelectionChanged="MainLongListSelector_SelectionChanged"> 
    <phone:LongListSelector.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <Image Name="PlayPause" Source="/Assets/transport.play.png"/> 
        <StackPanel> 
         <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/> 
        </StackPanel> 
       </StackPanel> 
      </DataTemplate> 
    </phone:LongListSelector.ItemTemplate> 
</phone:LongListSelector> 
+0

爲什麼不讓項目列出每個對象都有不同圖像的列表? –

+0

這是我從服務器獲得的曲目列表,並且圖像「播放\暫停」對於每個對象都必須相同,並且當用戶點擊時,我需要更改圖像以「暫停」選定的對象。圖像應該在VM實體中?如何做到這一點你說什麼? –

回答

0
<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'隨機或根據您的需要使用圖像。

+0

在那種情況下,我如何設置textblock的文本? –

+0

請告訴我你想要做什麼? –

+0

查看最後我對第一條消息的評論。 –