WPF将图片添加到ListBox

时间:2015-05-03 15:26:19

标签: wpf image xaml bitmap listbox

有一项任务:用户选择图像较少的文件夹。必须将这些图片添加到ListBox中。我知道如何将图像设置为集合列表,但我不知道如何动态添加图片到ListItem。请提出想法!

XAML:

<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="Auto"></RowDefinition>
    <RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Menu Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center">
    <MenuItem Header="Open" Width="50" Click="MenuItem_Click" FlowDirection="LeftToRight"/>
</Menu>
<ListBox Name="ListBox1" Grid.Row="1" Background="Yellow">            
</ListBox>

C#

public partial class MainWindow : Window
{
     IList<Bitmap> images = new List<Bitmap>();        

     public MainWindow()
     {
         InitializeComponent();
     }

     private void MenuItem_Click(object sender, RoutedEventArgs e)
     {
         var dialog = new System.Windows.Forms.FolderBrowserDialog();
         System.Windows.Forms.DialogResult result = dialog.ShowDialog();            

         if (result == System.Windows.Forms.DialogResult.OK)
         {
            foreach (string file in Directory.GetFiles(dialog.SelectedPath))
            {
                 images.Add(new Bitmap(file));

                 // ListBox1.Items.Add() // I Don't know how to add picture
            }
         }
     }
 }

0 个答案:

没有答案