/*
MainWindow.xaml
<Window x:Class="PhotoViewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="211" Width="563">
<Grid>
<ListBox x:Name="PhotoListBox" ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Height="{Binding PhotoListBox.Height}" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="20" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Image Source="{Binding FilePath}" HorizontalAlignment="Center" />
<TextBlock Grid.Row="1" Text="{Binding Caption}" HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
*/
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace PhotoViewer
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
private ObservableCollection<PhotoInfo> photos = new ObservableCollection<PhotoInfo>();
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
this.PhotoListBox.ItemsSource = photos;
}
private static bool IsImageFile(string fileName)
{
// 適当
var ext = Path.GetExtension(fileName).ToLowerInvariant();
return ext == ".png" || ext == ".jpg";
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
foreach (var path in Directory.EnumerateFiles(".").Where(IsImageFile))
{
Dispatcher.BeginInvoke(new Action(() =>
{
photos.Add(new PhotoInfo
{
FilePath = Path.GetFullPath(path)
});
}));
}
});
}
}
public class PhotoInfo
{
public string FilePath { get; set; }
public string Caption
{
get
{
return Path.GetFileName(FilePath);
}
}
}
}