0

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

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

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 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
4

1 回答 1

0

首先,您需要在 datagridview 上打开事件“CellClick”并输入以下代码:

 If (e.ColumnIndex <> -1) And (e.RowIndex <> -1) Then
       Dim selectedCell as string = ""
       selectedCell = myGridView.rows(e.rowIndex).cells("numberOfCell").valeu 
 End If
  • 如果这个答案对你有帮助。请在正确答案处标记。
于 2016-05-02T14:07:33.880 回答