0

我将 a 绑定ListBoxObservableCollection具有属性的项目中,该Image属性返回一个Image. 但是,我没有得到图像列表,而是得到了这个:

在此处输入图像描述

有什么建议么?

xml:

<Window x:Class="ObservableCollectionDirectoryListBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <DockPanel>

        <ListBox DockPanel.Dock="Top"
                 Name="listBox"
                 ItemsSource="{Binding Source={StaticResource infos}}"
                 DisplayMemberPath="Image"/>

    </DockPanel>

</Window>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading;

namespace ObservableCollectionDirectoryListBox
{
    public class ImageFileInfo : FileSystemInfo
    {
        public Image Image
        {
            get
            {
                var image = new Image() { Width = 100 };

                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.UriSource = new Uri(this.FullName);
                bitmapImage.DecodePixelWidth = 100;
                bitmapImage.EndInit();

                image.Source = bitmapImage;

                return image;
            }
        }

        public ImageFileInfo(string path)
            : base()
        {
            FullPath = path;
        }

        public override void Delete()
        {
            throw new NotImplementedException();
        }

        public override bool Exists
        {
            get { throw new NotImplementedException(); }
        }

        public override string Name
        {
            get { throw new NotImplementedException(); }
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            var infos = new ObservableCollection<ImageFileInfo>();

            Resources.Add("infos", infos);

            InitializeComponent();

            new DirectoryInfo(@"C:\Users\dharmatech\Pictures\From dharmatech\Camera roll")
            .GetFiles("*.jpg")
            .ToList()
            .ForEach(file => infos.Add(new ImageFileInfo(file.FullName)));

            var fileSystemWatcher =
                new FileSystemWatcher(@"C:\Users\dharmatech\Pictures\From dharmatech\Camera roll") 
                { EnableRaisingEvents = true };

            var context = SynchronizationContext.Current;

            fileSystemWatcher.Created += (s, e) =>
                context.Post(val =>
                {
                    if ((File.GetAttributes(e.FullPath) & FileAttributes.Normal) ==
                        FileAttributes.Normal)
                        infos.Add(new ImageFileInfo(e.Name));
                }, true);
        }
    }
}
4

2 回答 2

3

你不应该真的有一个集合,Images它是一个控件并且不属于那里,而是它应该是一个集合ImageSource,然后在 a DataTemplate( ListBox.ItemTemplate) 中可以绑定到Sourcean Image

于 2012-05-17T14:00:08.640 回答
1

我能够使用DataTemplate. 下面是 Xaml 代码ListBox

<ListBox DockPanel.Dock="Top"
         Name="listBox"
         ItemsSource="{Binding Source={StaticResource infos}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding Path=FullName}" Height="35"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
于 2012-05-17T16:07:25.523 回答