积极答复者
WPF ListBox 绑定图片

问题
答案
全部回复
-
图片数量:757
图片总大小:101M
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Click="Button_Click" >获取图片</Button>
<ListBox Grid.Row="1" Name="lbxImages" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Image Source="{Binding ImgPath}" Grid.Row="0" Width="130"/>
<TextBlock Text="{Binding ImgName}" Grid.Row="1" Width="130"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>cs:
public partial class MainWindow : Window
{
List<Images> list = new List<Images>();
public MainWindow()
{
InitializeComponent();
}private void BindImg()
{
string path = @"F:\图片\背景";
DirectoryInfo di = new DirectoryInfo(path);
foreach (var i in di.GetFiles())
{
Images img = new Images();
img.ImgPath = i.FullName;
img.ImgName = i.Name;
list.Add(img);
}
this.lbxImages.DataContext = list;
}private void Button_Click(object sender, RoutedEventArgs e)
{
BindImg();
}
}
public class Images
{
public string ImgPath { get; set; }
public string ImgName { get; set; }
}把 ListBox 的 WrapPanel 模板取消,便会很快,但加上 WrapPanel,问题就出现了(图片加载太慢)。