Reputation: 205
I have a ListView Control witch item is composite by two TextBlocks like that:
<ListView x:Name="resultsList" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="470" Margin="10,0,0,0" VerticalAlignment="Top" Width="342" FontSize="21.333" BorderThickness="0" ItemClick="RedirectPage" IsItemClickEnabled="True" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,-4,0,-4">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="0,0,0,0" />
</Style>
</StackPanel.Resources>
<TextBlock FontSize="35" Text="{Binding target_name}" />
<TextBlock FontSize="15" Text="{Binding type_name}" Opacity="30" Margin="0, 0, 0 ,30"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
In the code behind I am trying to get as string the text value from first TextBlock element.
string targetName;
targetName = resultsList.SelectedItem.ToString();
I want that Variable "targetName" то assign the text value ( Text="{Binding target_name}" ) from first TextBlock in the ListView Item.
I will be very thankful if someone is able to help me.
Upvotes: 1
Views: 1825
Reputation: 56
Itemssource should be collection. You need to typecast the collection to get the value.
class listData
{
public string target_name { get; set; }
public string type_name{ get; set; }
}
You can get values like this
listData ld= (listData)resultsList.SelectedItem;
string targetName =ld.target_name;
Upvotes: 1
Reputation: 2475
resultsList.SelectedItem should be the object from the collection that is bound to the ListView. So, you can just cast it to the appropriate type and then access the target_name member
Upvotes: 1