UWP ListView单击的项目[重复]

最后发布: 2018-05-17 14:40:57


问题

这个问题已经在这里有了答案:

我需要UWP中的ListView的帮助。 如何获取被单击项的TextBlock值? 先感谢您

我的代码:

     <ListView x:Name="lstVLocalTemp" ItemsSource="{Binding LocalTemp}" Background="{StaticResource NuhmeBackgroundColor}" IsItemClickEnabled="True" >
          <ListView.ItemTemplate>
                <DataTemplate x:Name="DoubleLineDataTemplate">
                     <StackPanel Orientation="Horizontal" Height="64" >
                          <Image Source="/Assets/Icons/Weather/Thermometer.png" Height="32" Width="32" VerticalAlignment="Center"/>
                          <StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="2,0,0,0">
                               <TextBlock x:Name="txBLocalTempLocation" Text="{Binding Location}"  Foreground="OrangeRed"/>
                               <TextBlock x:Name="txBLocalTempIP" Text="{Binding IP}" Foreground="DarkSeaGreen"/>
                               <TextBlock x:Name="txBLoacalTempDevice" Text="{Binding Device}" Foreground="DarkSeaGreen"/>
                          </StackPanel>
                    </StackPanel>
               </DataTemplate>
         </ListView.ItemTemplate>
    </ListView>

    Dim lt As New LocalTemp With {.Location = "Her", .IP = "192.168.0.155", .Device = TempDeviceType.DS18B20}
    Dim lt2 As New LocalTemp With {.Location = "Ikke hjemme", .IP = "192.168.0.156", .Device = TempDeviceType.DHT11}
    dataList.Add(lt)
    dataList.Add(lt2)
    lstVLocalTemp.ItemsSource = dataList

Public Class LocalTemp
    Public Property Location() As String
    Public Property IP() As String
    Public Property Device() As TempDeviceType
End Class
c# vb.net uwp
回答

实际上, TextBlock的值仅与LocalTemp对象绑定。 获取当前单击的项目对应的LocalTemp对象,可以获取每个值。 只需使用ItemClick事件句柄即可捕获当前单击的项目。 例如,

<ListView x:Name="lstVLocalTemp"  ItemClick="lstVLocalTemp_ItemClick" IsItemClickEnabled="True" >
...
</ListView>

Private Sub lstVLocalTemp_ItemClick(sender As Object, e As ItemClickEventArgs)
    Dim result As LocalTemp = e.ClickedItem
    Dim currentlocation As String = result.Location
    Dim currentIP As String = result.IP
End Sub

如果我误解了您,请纠正我;或者,如果您实际上对单击的项目有特殊要求,请详细说明。