简体   繁体   中英

XAML Get Item text value from ListViewItem

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.

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM