将图像加载到ListBox中

时间:2012-02-15 07:07:28

标签: c# wpf

我想在WPF中将图像显示到ListBox中。我想要实现的是

  1. 从设备中读取图像并获取名称等信息(完成)
  2. 在列表框中创建项目并显示默认拇指(某些默认图片)代替实际拇指,因为稍后将为图像阅读拇指。 (DONE)
  3. 现在获取缩略图并将其存储在一个文件夹(DONE)中。由任务完成。

  4. 现在显示图像的实际拇指。 (未完成)。

  5. 第4步是我陷入困境的地方。基本上我想做的是类似于窗口,它将首先显示图像/视频图标然后用拇指填充。

    请帮忙。我是WPF的新手,因此面临问题。

3 个答案:

答案 0 :(得分:1)

    <ListBox ItemsSource="{Binding Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="5">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="10"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Image Source="{Binding Image}"/>
                    <Grid Grid.Column="2">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Grid.Row="1" Text="{Binding Desc}"/>
                        <TextBlock Grid.Row="2" Text="{Binding Notes}"/>
                    </Grid>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

public partial class ImageListBox : INotifyPropertyChanged
{
    private ObservableCollection<Item> _items;
    public ObservableCollection<Item> Items
    {
        get { return _items; }
        set { _items = value; OnPropertyChanged("Items"); }
    }

    public ImageListBox()
    {
        DataContext = this;
        Items = new ObservableCollection<Item>(new List<Item>
                                                     {
                                                         new Item { Image = "Images/_(1).png" , Desc = "Desc1",Name = "Name1",Notes = "Notes1"},
                                                         new Item { Image = "Images/_(2).png" , Desc = "Desc2",Name = "Name2",Notes = "Notes2"},
                                                         new Item { Image = "Images/_(3).png" , Desc = "Desc3",Name = "Name3",Notes = "Notes3"},
                                                         new Item { Image = "Images/_(4).png" , Desc = "Desc4",Name = "Name4",Notes = "Notes4"},
                                                         new Item { Image = "Images/_(5).png" , Desc = "Desc5",Name = "Name5",Notes = "Notes5"},
                                                     });
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
    }
}

public class Item
{
    public String Image { get; set; }
    public String Name { get; set; }
    public String Desc { get; set; }
    public String Notes { get; set; }
}

答案 1 :(得分:1)

我可能无法正确理解您的问题,但如果我做对了:您希望在某些WPF控件中显示图像。

1)将图像的路径绑定到控件(我使用DataGrid):

 <DataGrid ItemsSource="{Binding Path=ImageCollection}"
        <DataGrid.Columns>                    
                            <DataGridTemplateColumn Header="Image">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Image Width="40" Height="20" Source="{Binding FilePath, Converter={StaticResource ResourceKey=ImageConverter}}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
</DataGrid>

2)创建将FilePath转换为ImageSource的转换器(我在Internet上的某个地方找到它):

 public class ImageConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var path = value as string;

                if (path == null)
                {
                    return DependencyProperty.UnsetValue;
                }
                //create new stream and create bitmap frame
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                try
                {
                    bitmapImage.StreamSource = new FileStream(path, FileMode.Open, FileAccess.Read);
                    //load the image now so we can immediately dispose of the stream
                    bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                    bitmapImage.EndInit();
                    //clean up the stream to avoid file access exceptions when attempting to delete images
                    bitmapImage.StreamSource.Dispose();
                    return bitmapImage;
                }
                catch (Exception)
                {        
                    //do smth
                }

            }
}

希望,这有帮助。

答案 2 :(得分:0)

有各种方法,一种是使用PriorityBinding,首先显示占位符图标,然后是拇指。 (您可能需要在完全加载后才分配保存图像的属性。)