2016-07-23 20 views
1

Ich habe eine ComboBox, die ein Bild und Text zusammen mit ihm in einem Stackpanel zeigt. Die Elemente werden angezeigt, wenn ich die ComboBox anfänglich öffne. Wenn ich in der Liste nach unten scrolle, verschwinden die Bilder in den Elementen am Anfang der Liste (wenn ich zurück scrolle, um diese Elemente zu sehen) und umgekehrt. Der Text bleibt erhalten. Auch wenn ich ohne Scrollen ein Element aus der Combobox auswähle, wird dieses Element ohne das Bild in der geschlossenen Combobox angezeigt. Wie behebe ich das?Bilder verschwinden in Combobox

<ComboBox ItemsSource="{Binding ElementName=searchPage, Path=emotionList}" 
           SelectionChanged="ComboBox_SelectionChanged" 
           Name="emotionComboBox" 
           VerticalAlignment="Center"> 
          <ComboBox.ItemTemplate> 
           <DataTemplate x:DataType="local:StorageItemThumbnailClass"> 
            <StackPanel Orientation="Horizontal"> 
             <Image Source="{Binding Thumbnail, Converter={StaticResource ImagetoThumbnailConverter}, Mode=OneWay}" Margin="10" MaxHeight="50" MaxWidth="50"/> 
             <TextBlock Text="{Binding Name}" Style="{StaticResource BodyTextBlockStyle}" Margin="10" TextWrapping="WrapWholeWords" Width="120"/> 
            </StackPanel> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 

Diese Methode aus der OnNavigated Funktion des searchPage genannt wird, worin die Combobox vorhanden ist -

private async Task populateEmotionListAsync() 
     {    
      emotionList = new ObservableCollection<StorageItemThumbnailClass>(); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.None.ToString(), Thumbnail = null }); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.Angry.ToString(), Thumbnail = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/EmotionIcons/angry.png"))).GetThumbnailAsync(ThumbnailMode.PicturesView, 50) }); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.Contempt.ToString(), Thumbnail = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/EmotionIcons/contempt.png"))).GetThumbnailAsync(ThumbnailMode.PicturesView, 50) }); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.Disgusted.ToString(), Thumbnail = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/EmotionIcons/disgust.png"))).GetThumbnailAsync(ThumbnailMode.PicturesView, 50) }); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.Afraid.ToString(), Thumbnail = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/EmotionIcons/afraid.png"))).GetThumbnailAsync(ThumbnailMode.PicturesView, 50) }); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.Happy.ToString(), Thumbnail = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/EmotionIcons/happy.png"))).GetThumbnailAsync(ThumbnailMode.PicturesView, 50) }); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.Neutral.ToString(), Thumbnail = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/EmotionIcons/neutral.png"))).GetThumbnailAsync(ThumbnailMode.PicturesView, 50) }); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.Sad.ToString(), Thumbnail = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/EmotionIcons/sad.png"))).GetThumbnailAsync(ThumbnailMode.PicturesView, 50) }); 
      emotionList.Add(new StorageItemThumbnailClass { Name = EmotionEnum.Surprised.ToString(), Thumbnail = await (await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/EmotionIcons/surprised.png"))).GetThumbnailAsync(ThumbnailMode.PicturesView, 50) }); 
     } 

ist die StorageItemThumbnailClass -

public class StorageItemThumbnailClass : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private StorageItemThumbnail _thumbnail; 
    private string _name; 

    public StorageItemThumbnail Thumbnail 
    { 
     get { return _thumbnail; } 
     set 
     { 
      _thumbnail = value; 
      // Call OnPropertyChanged whenever the property is updated 
      OnPropertyChanged("Thumbnail"); 
     } 
    } 

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 
    } 

    public String Name 
    { 
     get { return _name; } 
     set 
     { 
      _name = value; 
      // Call OnPropertyChanged whenever the property is updated 
      OnPropertyChanged("Name"); 
     } 
    } 
} 

Und hier ist die Konverter-

Antwort

1

Ich konnte dieses Problem beheben. Die ComboBox führt eine UI-Virtualisierung durch, und Bilder im virtualisierten Panel der ComboBox wurden entfernt, wenn sie nicht in der Ansicht gescrollt wurden. Beim Zurückscrollen wurde der Konverter des entfernten Bildes erneut aufgerufen und die Bildquelle wurde zurückgesetzt. Daher musste der als Quelle verwendete Datenstrom zur Wiederverwendung auf die Startposition gesetzt werden.

Converter-

StorageItemThumbnail thumbnail = (StorageItemThumbnail)value; 
thumbnail.Seek(0); 
image = new BitmapImage(); 
image.SetSource(thumbnail);