1
votes

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.

2
do you have some knowledge about MVVM? โ€“ Bruno Joaquim
you can retrieve this value very easily if you opt to use MVVM. โ€“ Bruno Joaquim

2 Answers

1
votes

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

1
votes

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;