A simple image listview
I am trying to make a simple dictionary bound image listview where key is filename
and value is image path
.
Following is the code
<ListView Grid.Column="2" Grid.Row="3" Height="131" HorizontalAlignment="Left" Margin="6,9,0,0" Name="GenreListView1" VerticalAlignment="Top" Width="375">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"></WrapPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"></Image>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Following is the code for runtime:
Dim maindir As DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(CurDir() + "\Icons")
GenreDictionary.Clear()
For Each k As FileInfo In maindir.GetFiles()
If k.Name.EndsWith(".png") Then
GenreDictionary.Add(k.Name, k.FullName)
End If
Next
'Load Icons to Genre View
GenreListView1.ItemsSource = GenreDictionary.Values
Could you please guide me on how to get the image bound. This is a windo开发者_运维问答ws app.
Why are you using a dictionary? Why not just use a list of FileNames? If you do that then your <Image Source="{Binding"} />
would work. Your code would then look like this:
Dim maindir As DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(CurDir() + "\Icons")
Dim fileList = as new List<string>();
For Each k As FileInfo In maindir.GetFiles()
If k.Name.EndsWith(".png") Then
fileList.Add(k.FullName)
End If
Next
'Load Icons to Genre View
GenreListView1.ItemsSource = fileList
Try
<Image Source="{Binding Value}"/>
精彩评论