2016-07-23 11 views
1

私は画像とテキストをスタックパネルに表示するComboBoxを持っています。 ComboBoxを最初に開いたときにアイテムが表示されます。リストをスクロールダウンすると、リストの一番上にあるアイテムのイメージが消えます(それらのアイテムを表示するためにスクロールして戻ると)、その逆もあります。テキストは元のままです。また、スクロールしなくても、コンボボックスから項目を選択すると、その項目は閉じられたコンボボックスに画像なしで表示されます。これを是正する方法は?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> 

このメソッドは、コンボボックスが存在するsearchPageのOnNavigated関数から呼び出された - ここで

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) }); 
     } 

は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"); 
     } 
    } 
} 

そして、ここconverter-です

答えて

1

この問題を解決できました。 ComboBoxはUI仮想化を実行し、ComboBoxの仮想化されたパネル内の画像は、表示外にスクロールすると削除されました。スクロールバックすると、削除されたイメージのコンバータが再び呼び出され、イメージソースがリセットされました。したがって、ソースとして使用されるストリームは、再利用のために開始位置に設定する必要がありました。

Converter-

StorageItemThumbnail thumbnail = (StorageItemThumbnail)value; 
thumbnail.Seek(0); 
image = new BitmapImage(); 
image.SetSource(thumbnail); 
関連する問題