Using the following XAML how can I retrieve the value of let’s say <TextBlock/SenderName> or any other control nested in the stackpanel depending on the listview current selected item? in C#
XAML
<ListView x:Name="ItemListView"
ItemsSource="{Binding}"
Margin="1041,120,85,68" SelectionChanged="ListView1_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding SenderName}"
FontSize="20" Margin="15,0,0,0" HorizontalAlignment="Right" />
<TextBlock Text="{Binding Value}"
FontSize="18" Margin="5,0,0,0" TextWrapping="Wrap" HorizontalAlignment="Right" />
<TextBlock Visibility="Collapsed" Text="{Binding StreamId}" />
<TextBlock Text="{Binding DateTime}"
FontSize="16" Margin="15,0,0,0" HorizontalAlignment="Right" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
If you’re using a MVVM approach then your ListView’s ItemsSource is bound to a property on your ViewModel. To gain access to values from the ListBox’s SelectedItem bind the SelectedItem to another member on your view model. Something like:
Code:
XAML:
If you’re looking to do it from the code-behind, you can access the
SelectedItemmember of ItemListView which should be the object the currently selected item is bound to.Finally, if you want to bind to the control from another control you can access it using ElementName and SelectedItem