0

I need help with a ListView in UWP. How do I get the TextBlock value of the clicked item? Thank you in advance

My code:

     <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
henrikl2000
  • 37
  • 1
  • 5
  • I'm seeing this a lot lately and I'm not sure why. I believe people are either deciding to learn UWP or the demand is going up but learning MVVM and XAML is strongly recommended when learning UWP or WPF. I believe this is an issue of code behind and XAML binding, all done the wrong way. You can surely write UWP in an MVC format, just fine, but when you're DataBinding you're more of a MVVM format and the two shouldn't be combined. I've answered these questions but I'm starting to feel that I'm not really helping anyone. IMO, you should take a short break and learn more about MVVM / XAML. – Michael Puckett II May 17 '18 at 15:37

1 Answers1

0

Actually the values of TextBlock are just binding with LocalTemp object. Get the current clicked item correspond LocalTemp object, you could get each value. Simply using ItemClick event handle to catch the current clicked item. For example,

<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

If I misunderstand you please correct me, or if actually you have special demand on the clicked item please detail that.

Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21