WPF VB.Net Datagrid在FullRow选择模式下来自特定Cell的值?

时间:2016-05-02 13:18:28

标签: wpf vb.net data-binding wpfdatagrid fullrowselect

如何使用选择模式FullRow从Datagrid中的特定Cell获取值?

目前我使用此命令,但如果我手动选择单元格,它只显示值:

Public Sub MenuItem_Click(sender As Object, e As RoutedEventArgs)
    Dim selectedData As String = ""
    Dim wert As String = ""
    For Each dataGridCellInfo In myGridView.SelectedCells()
        Dim pi As Reflection.PropertyInfo = dataGridCellInfo.Item.[GetType]().GetProperty(dataGridCellInfo.Column.Header.ToString())
        Dim value = pi.GetValue(dataGridCellInfo.Item, Nothing)
        selectedData += dataGridCellInfo.Column.Header + ": " + value.ToString() + vbLf
        wert = value.ToString
    Next
    MessageBox.Show(selectedData)

End Sub

但我不想一直手动选择单元格。 我只想点击该行中的任何单元格,并始终获取单元格“Pfad”中的值并将其放入字符串中。

XAML DataGrid代码:

<DataGrid Name="myGridView" SelectionUnit="Cell" IsReadOnly="True" ItemsSource="{Binding}" Grid.ColumnSpan="3" Background="#FFA4A4A4" BorderThickness="2" BorderBrush="#FF6A6F77" AutoGenerateColumns="False" Margin="10,31,10,149.714" GridLinesVisibility="None">
        <DataGrid.Columns>
            <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.Columns>
    </DataGrid>

代码隐藏:

    Public Structure Austauscher

    Dim files As New ObservableCollection(Of Austauscher)

    Private _track As String
    Private _image As BitmapImage
    Private _album As String
    Private _pfad As String
    Private _bitrate As String
    Private _endung As String


    Property Track() As String
        Get
            Return _track
        End Get
        Set(ByVal Value As String)
            _track = 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
    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

End Structure

1 个答案:

答案 0 :(得分:0)

首先,您需要开启活动&#34; CellClick&#34;在您的datagridview上并输入以下代码:

 If (e.ColumnIndex <> -1) And (e.RowIndex <> -1) Then
       Dim selectedCell as string = ""
       selectedCell = myGridView.rows(e.rowIndex).cells("numberOfCell").valeu 
 End If
  • 如果本答案对您有所帮助。请在正确的答案中注明这一点。