Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF列表框不连续绑定_C#_Wpf - Fatal编程技术网

C# WPF列表框不连续绑定

C# WPF列表框不连续绑定,c#,wpf,C#,Wpf,我有一个列表框,执行自动完成功能,动态填充。列表框显示带有员工图片的姓名列表。我发现用图像填充数据很慢 我希望能够首先填充名称,然后在收到数据时异步上传数据。我该怎么做呢 目前我的图像类代码: public class Img : INotifyPropertyChanged { private string name; private Image image; public event PropertyChangedEventHandler PropertyChange

我有一个列表框,执行自动完成功能,动态填充。列表框显示带有员工图片的姓名列表。我发现用图像填充数据很慢

我希望能够首先填充名称,然后在收到数据时异步上传数据。我该怎么做呢

目前我的图像类代码:

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>
目前,它同时填充所有的名称+图像…这导致列表框速度慢得令人无法忍受


提前感谢

好的,延迟加载图像需要做两件事

按先慢加载,然后快加载的顺序进行优先绑定。 每个图像的DownloadCompleted事件必须通知绑定的更改,即它已准备好显示。 看看这两篇文章,看看你是否能得到一个提示

让我知道这是否有帮助