Das geht auch in WPF sehr gut und relativ einfach:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:DirectoryToImagesConverter _
x:Key="Dir2ImgConv"></local:DirectoryToImagesConverter>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding SelectedItem, ElementName=trvFiles,
Converter={StaticResource Dir2ImgConv}}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Margin="219,12,12,12"
Name="lstThumbnails">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"
Width="70"
Height="70"
Margin="20,20"></Image>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"
Orientation="Horizontal">
</WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
<TreeView Margin="12,12,0,12"
HorizontalAlignment="Left"
Width="201"
Name="trvFiles">
</TreeView>
</Grid>
</Window> Class MainWindow
Private Sub Window_Loaded(sender As System.Object, e As _
System.Windows.RoutedEventArgs) Handles MyBase.Loaded
For Each drv In My.Computer.FileSystem.Drives
Dim dir As New IO.DirectoryInfo(drv.Name)
Dim trv As New TreeViewItem()
trv.Header = dir.Name
trv.DataContext = dir
Me.FillTreeViewItem(trv, dir)
trvFiles.Items.Add(trv)
Next
End Sub
Private Sub trvFiles_SelectedItemChanged(sender As System.Object, e As _
System.Windows.RoutedPropertyChangedEventArgs(Of System.Object)) Handles _
trvFiles.SelectedItemChanged
If e.NewValue IsNot Nothing AndAlso TypeOf e.NewValue Is TreeViewItem _
Then
Dim trv = CType(e.NewValue, TreeViewItem)
If trv.Items.Count = 0 AndAlso _
trv.DataContext IsNot Nothing AndAlso TypeOf trv.DataContext Is _
IO.DirectoryInfo Then
Dim dir = CType(trv.DataContext, IO.DirectoryInfo)
Me.FillTreeViewItem(trv, dir)
End If
End If
End Sub
Private Sub FillTreeViewItem(trv As TreeViewItem, dir As IO.DirectoryInfo)
Try
For Each d In dir.GetDirectories
trv.Items.Add(New TreeViewItem With {.Header = d.Name, _
.DataContext = d})
Next
For Each f In dir.GetFiles
trv.Items.Add(New TreeViewItem With {.Header = f.Name, _
.DataContext = f})
Next
Catch ex As IO.IOException
Debug.WriteLine("Cannot read: " & dir.FullName)
End Try
End Sub
End Class
Public Class DirectoryToImagesConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, _
parameter As Object, culture As System.Globalization.CultureInfo) As _
Object Implements System.Windows.Data.IValueConverter.Convert
If value IsNot Nothing AndAlso TypeOf value Is TreeViewItem Then
Dim trv = CType(value, TreeViewItem)
If trv.DataContext IsNot Nothing AndAlso TypeOf trv.DataContext Is _
IO.DirectoryInfo Then
Dim dir = CType(trv.DataContext, IO.DirectoryInfo)
Dim lst As New List(Of ImageSource)
For Each file In dir.GetFiles("*.jpg")
lst.Add(New BitmapImage(New Uri(file.FullName, _
UriKind.Absolute)))
Next
Return lst.ToArray
Else
Return Nothing
End If
Else
Return Nothing
End If
End Function
Public Function ConvertBack(value As Object, targetType As System.Type, _
parameter As Object, culture As System.Globalization.CultureInfo) As _
Object Implements System.Windows.Data.IValueConverter.ConvertBack
Throw New NotSupportedException
End Function
End Class Maas |