UWP ListView点击了项目

时间:2018-05-17 14:40:57

标签: c# vb.net uwp

我需要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

1 个答案:

答案 0 :(得分:0)

实际上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

如果我误解你,请纠正我,或者如果您对点击的项目有特殊要求,请详细说明。