WPF VB.net Listview如何显示所选单元格的值

时间:2016-04-25 17:26:29

标签: wpf vb.net listview data-binding

首先,我是使用WPF进行编程的新手,我在WPF中使用listview时遇到了一些困难。 我已经有一个ObservableCollection,并希望在MsgBox中显示所选Cell的值。

我的问题是,我应该怎么做?

这对我不起作用:

mylistview.SelectedItem()

我的代码:

XAML

<ListView x:Name="mylistView" Margin="10,31,10,149.714" SelectionMode="Multiple" ItemsSource="{Binding}" Grid.ColumnSpan="3" Background="#FFA4A4A4" BorderThickness="2" BorderBrush="#FF6A6F77">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Track}" Width="100" Header="Track"/>
            <GridViewColumn Width="auto" Header="" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Image Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding Path=Image}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Endung}" Width="100" Header=" Container"/>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Album}" Width="100" Header="Album"/>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Bitrate}" Width="100" Header="Bitrate"/>
            <GridViewColumn DisplayMemberBinding="{Binding Path=Pfad}" Width="100" Header="Pfad"/>
        </GridView>
    </ListView.View>
</ListView>

代码隐藏

Dim files As New ObservableCollection(Of Austauscher) 

    Public Structure Austauscher
    Private _track As String
    Private _album As String
    Private _pfad As String
    Private _bitrate As String
    Private _endung As String
    Private _image As BitmapImage

    Property Track() As String
        Get
            Return _track
        End Get
        Set(ByVal Value As String)
            _track = Value
        End Set
    End Property
    Property Album() As String
        Get
            Return _album
        End Get
        Set(ByVal Value As String)
            _album = Value
        End Set
    End Property
    Public Property Pfad As String
        Get
            Return _pfad
        End Get
        Set(ByVal Value As String)
            _pfad = Value
        End Set
    End Property
    Property Bitrate As String
        Get
            Return _bitrate
        End Get
        Set(ByVal Value As String)
            _bitrate = Value
        End Set
    End Property
    Property Endung As String
        Get
            Return _endung
        End Get
        Set(ByVal Value As String)
            _endung = Value
        End Set
    End Property
    Property Image As BitmapImage
        Get
            Return _image
        End Get
        Set(ByVal Value As BitmapImage)
            _image = Value
        End Set
    End Property

End Structure

我添加文件:

files.Add(New Austauscher With {.Track = "Track", .Pfad = path, .Album = "Album", .Bitrate = "kbs", .Endung = "extension"})

1 个答案:

答案 0 :(得分:0)

问题在于listView,因为您发现它缺少“selectedCell”属性。

您可能会发现使用datagrid控件可以获得所需的功能,我创建的版本应该与listview示例相同;

public function getStartDateAttribute() {
    return Carbon::parse($this->start);
}

datagrid提供了对选择的更多控制,并包含一个名为selectedCells的属性,它是一个集合,因为(如在您的示例中)您可以选择许多单元格。如果你想只有一个选定的单元格,那么你应该将SelectionMode属性设置为single(允许你只选择一个项目并将SelectionUnit(如我的例子中所示)设置为Cell。另外,你可以使用返回的CellCell属性有焦点的细胞;

        <DataGrid Name="myGridView"  SelectionMode="Extended" SelectionUnit="Cell" ItemsSource="{Binding}" Grid.ColumnSpan="3" Background="#FFA4A4A4" BorderThickness="2" BorderBrush="#FF6A6F77" AutoGenerateColumns="False">
        <DataGridTextColumn Header="Track" Binding="{Binding Path=Track}" Width="100"/>
        <DataGridTemplateColumn Header="">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding Path=Image}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Binding="{Binding Path=Endung}" Width="100" Header=" Container"/>
        <DataGridTextColumn Binding="{Binding Path=Album}" Width="100" Header="Album"/>
        <DataGridTextColumn Binding="{Binding Path=Bitrate}" Width="100" Header="Bitrate"/>
        <DataGridTextColumn Binding="{Binding Path=Pfad}" Width="100" Header="Pfad"/>
    </DataGrid>

我希望这会有所帮助。