简体   繁体   中英

Access TextBlock Text inside a DataTemplate UWP

I searched all over the internet on this question, but I can't made progress in my UWP application.

I have a ListView with a DataTemplate :

<ListView x:Name="lvEszTmplt" Margin="50,61,0,0" ItemClick="lvEszTmplt_ItemClick_1" SelectionChanged="lvEszTmplt_SelectionChanged_1"   >
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="0,8" x:Name="spItem" >
                    <TextBlock x:Name="tbAzonosito" > 
                                <Run Text="Azonosító: " Foreground="DarkGray"/> 
                                <Run Text="{Binding Azonosito}" 
                                     Foreground="DarkGray"
                                     />

                    </TextBlock>
                    <TextBlock x:Name="tbMegnevezes" > 
                                <Run Text="Megnevezés: " Foreground="DarkGray"/> 
                                <Run Text="{Binding Megnevezes}"
                                     Foreground="DarkGray"/>

                    </TextBlock>
                    <TextBlock x:Name="tbSerial" > 
                                <Run Text="Serial: " Foreground="DarkGray"/> 
                                <Run Text="{Binding Serial}"
                                     Foreground="DarkGray"/>
                    </TextBlock>
                    <TextBlock x:Name="tbSorszam"   > 
                                <Run Text="Sorszám: " Foreground="DarkGray"/> 
                                <Run Text="{Binding Sorszam}"
                                     Foreground="DarkGray"/>
                    </TextBlock>

                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

In the ListView , the "Azonosito" is an ID column. When I click on a Item , it should go to another Frame and take the ID from the ListView , and load the Deatails View with the data from the ID.

How can I do this?

Thank you for helping me out!

Use ItemClickEventArgs.ClickedItem property of the ItemClick event to read the clicked item:

private void lvEszTmplt_ItemClick_1(object sender, ItemClickEventArgs e)
{
    var item = (ItemModel)e.ClickedItem;
    var id = item.Azonosito;
}

@Krzysztof Bracha have a good answer.But I see you forget add the IsItemClickEnabled property.

You can use IsItemClickEnabled=true to enabled ItemClick.

And you should bind the sources to ListView.

You also can use x:bind to binding the data.

If your sources' type is ObservableCollection that you can use the code as @Krzysztof Bracha.

If you need get the control in ListView that you can use VisualTreeHelper.GetChild(DependencyObject reference, int childIndex); to get it.

See: https://docs.microsoft.com/en-us/windows/uwp/data-binding/data-binding-in-depth

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