Image
속성을 갖는 항목 중 ObservableCollection
에 ListBox
을 바인딩하고 있는데, Image
을 반환합니다.ListBox를 ObservableCollection에 바인딩 할 때 DisplayMemberPath를 사용하여
어떤 제안 그러나, 대신 이미지의 목록을 받고, 나는이 얻을?
XAML :
<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 번호 : 당신은 정말 제어하고 거기에 속하지 않는 Images
의 컬렉션을하지 말았어야
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);
}
}
}
안녕하세요. H. WPF 4 Unleashed 책을 살펴 보았습니다. 그는 당신이 위에서 묘사 한 것과 같은 것을 제안합니다. 나는 실험을 위해서 다른 접근법을 시도하고 있었다. 모범 사례가 아니더라도 이미지가 왜 나타나지 않는지 이해하고 싶습니다. – dharmatech
@dharmatech : 아마도'ListBox'는'TextBox'를 만들고'Text'를 지정된'DisplayMemberPath'에 바인드 할 것입니다. 그러면 형식 이름을 반환하는'Image'에서'ToString'이 호출됩니다. 나는 왜 그들이 단지 ContentPresenter의'Content'를 바인딩하지 않는지 전혀 모른다. VisualTreeHelper (또는 Snoop과 같은 외부 도구)를 사용하여 VisualTree를 검사하여이 가설을 테스트 할 수 있습니다. –
OK, 팁 H.B 주셔서 감사합니다. – dharmatech