图像未显示在第一列Listview WPF中使用VB.NET(第一个Col图像,第二个Col字符串)

时间:2014-10-28 07:48:20

标签: wpf vb.net listview

我的项目需要专家帮助。 请帮助我,我是WPF的新手,我正在使用VB.NET创建应用程序来显示用户选择的文件夹中的图片。在Form I上放置Button(按下以选择文件夹),Image(显示从ListView中选择的图像),ListView(2列,第1列图像缩略图,第2列,文件名)。 请检查下面的XAML代码:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="MainWindow"
    Title="MainWindow" Height="448.545" Width="698.135">
    <Grid>
        <Image x:Name="ImgView" Margin="259,44,10,72"/>
        <Button Content="Pilih Folder" HorizontalAlignment="Left" Height="29" Margin="10,10,0,0" VerticalAlignment="Top" Width="92" Click="Button_Click"/>
        <ListView x:Name="ListView1" HorizontalAlignment="Left" Height="351" Margin="10,44,0,0" VerticalAlignment="Top" Width="244">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Gbr" DisplayMemberBinding="{Binding Gambar}"/>
                    <GridViewColumn Header="Nama File" DisplayMemberBinding="{Binding NamaFile}"/>
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>

这是我的代码隐藏

Imports System.Windows.Forms
Imports System.Collections.ObjectModel

Class MainWindow

    Private _GbrCollection As New ObservableCollection(Of GambarCol)

    Public ReadOnly Property GbrCollection() As ObservableCollection(Of GambarCol)
        Get
            Return _GbrCollection
        End Get
    End Property

    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        InitializeComponent()
    End Sub

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim ImgList() As Image
        Dim Img88 As Image
        Dim fld As New FolderBrowserDialog

        fld.RootFolder = Environment.SpecialFolder.Desktop
        'fldDialog.RootFolder = Environment.SpecialFolder.Desktop;
        fld.ShowDialog()
        'txtPath.Text = fld.SelectedPath

        'filesListBox.Items.Clear()

        Dim fileNames = My.Computer.FileSystem.GetFiles(fld.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, "*.jpg")
        'Debug.WriteLine(fileNames.Count)
        ReDim ImgList(fileNames.Count)
        For Each fileName As String In fileNames
            _GbrCollection.Add(New GambarCol(Img88, fileName))
        Next

        ListView1.ItemsSource = GbrCollection
    End Sub

    Private Sub ListView1_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles ListView1.MouseDoubleClick
        Dim ImgShow As New Image

        If ListView1.SelectedItems.Count > 0 Then


        End If
        'ImgShow.Source = New BitmapImage(New System.Uri(ListView1.Items))
    End Sub
    Private Sub ListView1_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ListView1.SelectionChanged

    End Sub
End Class
Public Class GambarCol
    Private _Gambar As Image
    Private _NamaFile As String

    Public ReadOnly Property Gambar() As Image
        Get
            _Gambar = New Image
            _Gambar.Source = New BitmapImage(New System.Uri(NamaFile))
            _Gambar.Height = 60
            Return _Gambar
        End Get
    End Property

    Public ReadOnly Property NamaFile() As String
        Get
            Return _NamaFile
        End Get
    End Property

    Public Sub New(ByVal GbrImg As Image, ByVal NamaFileNF As String)
        _Gambar = GbrImg
        _NamaFile = NamaFileNF
    End Sub
End Class

师父,帮我解决3个问题: 1.图像无法显示在Listview的第1列中 2.当用户双击所选行时,如何从第2列获取值 3.我知道我的代码太复杂了,有人可以帮我简单吗

真的需要帮助

非常感谢

1 个答案:

答案 0 :(得分:0)

我刚刚发现了解决方案,在多次引用其他文章后,我就是这样做的。 在StackPanel上构建的XAML代码用于保存第一个col的Image,第二个col保持

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

    <Grid>
        <ListView x:Name="ListView1"   VirtualizingStackPanel.IsVirtualizing="True"  ItemsSource="{Binding ListViewItemsCollections}" Margin="0,56,0,10" HorizontalAlignment="Left" Width="228">
            <ListView.View>
                <GridView AllowsColumnReorder="False">
                    <GridViewColumn x:Name="GridViewColumnName" Header="Picture"  Width="90">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image x:Name="Image_GridViewColumnName" Width="90" Height="50" Source="{Binding GridViewColumnNameImageSource}" />
                                    <Label Content="{Binding GridViewColumnNameLabelContent}" Width="50" Height="50"  />
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn x:Name="GridViewColumnLocation" Header="Location" Width="150" DisplayMemberBinding="{Binding GridViewColumnLocation}" />
                </GridView>
            </ListView.View>
        </ListView>
        <Button x:Name="btnFolder" Content="Pilih Folder" Margin="10,10,0,0" Height="41" VerticalAlignment="Top" HorizontalAlignment="Left" Width="77"/>
        <TextBox x:Name="txtPath" Height="23" Margin="112,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Width="116"/>
        <Image x:Name="ImgView" Margin="247,56,10,113"/>
    </Grid>
</Window>

而对于代码,只需使用带有NEW键的Add方法,坦率地说我还是不明白New背后的概念,因为WPF对我来说真的很新,VB.NET仍然不是全部100%我可以编码。 对于代码隐藏如下

Imports System.Windows.Forms.ListViewItem.ListViewSubItemCollection
Imports System.IO
Imports System.Windows.Window
Imports System.Windows.Forms
Imports System.Windows.Forms.FolderBrowserDialog

Class MainWindow
    Dim IsiFile() As String

    Private Sub btnFolder_Click(sender As Object, e As RoutedEventArgs) Handles btnFolder.Click
        Dim fld As New FolderBrowserDialog
        Dim iCnt As Integer = 0

        fld.RootFolder = Environment.SpecialFolder.Desktop
        fld.ShowDialog()
        txtPath.Text = fld.SelectedPath

        Dim fileNames = My.Computer.FileSystem.GetFiles(fld.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, "*.jpg")
        ReDim IsiFile(fileNames.Count)

        For Each fileName As String In fileNames
            ListView1.Items.Add(New With { _
    Key .GridViewColumnLocation = fileName, _
    Key .GridViewColumnNameImageSource = fileName _
    })
            IsiFile(iCnt) = fileName
            iCnt += 1
        Next
    End Sub

    Private Sub ListView1_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles ListView1.MouseDoubleClick
        Dim Gambar As New BitmapImage
        Gambar.BeginInit()
        Gambar.UriSource = New Uri(IsiFile(ListView1.SelectedIndex))
        Gambar.EndInit()
        ImgView.Source = Gambar
    End Sub
End Class

我希望这有帮助,谢谢