2011-08-22 64 views
0

我有一個列表框,執行動態填充的自動完成功能。列表框顯示員工照片的名稱列表。我發現用圖像填充數據很慢。WPF列表框異步綁定

我希望能夠先填充名稱,然後在收到數據時以異步方式上傳數據。我該如何去做?

此刻我的圖像類代碼:

public class Img : INotifyPropertyChanged 
{ 
    private string name; 
    private Image image; 
    public event PropertyChangedEventHandler PropertyChanged; 

    public Img(string name, Image image) 
    { 
     this.name = name; 
     this.image = image; 
    } 

    public string Name 
    { 
     get { return name; } 
     set 
     { 
      name = value; 
      OnPropertyChanged("PersonName"); 
     } 
    } 

    public Image Image 
    { 
     get { return image; } 
     set 
     { 
      image = value; 
      OnPropertyChanged("Image"); 
     } 
    } 

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 

用於填充數據的代碼:

 foreach (KeyValuePair<string, string> entry in items) 
     { 
      System.Windows.Controls.Image webImage = new System.Windows.Controls.Image(); 
      webImage.Dispatcher.Invoke(DispatcherPriority.Normal, 
       (ThreadStart)delegate 
       { 

        BitmapImage image = new BitmapImage(); 
        image.BeginInit(); 
        image.UriSource = new Uri(//Where the source is); 
        image.EndInit(); 

        webImage.Source = image; 
       }); 
      myListBox.Items.Add(new Img(entry.Value, webImage)); 
     } 

我的XAML代碼:

<Popup Name="myPopUp"> 
    <ListBox Name="myListBox" FontSize="14"> 
     <ListBox.ItemTemplate> 
      <DataTemplate DataType="{x:Type local:Img}"> 
       <StackPanel Orientation="Horizontal"> 
        <ContentPresenter Margin="3" Content="{Binding Image, IsAsync=True}"/> 
        <TextBlock Margin="3" Text="{Binding Name, IsAsync=True}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Popup> 

目前,它填充所有的名字+圖像在同一時間......這會導致列表框變得難以忍受。

在此先感謝

回答