Search code examples
c#uwpdatatemplate

How to access a specific item in itemscontrol and retrieve some data in UWP


I have an ItemsControl with DataTemplate in my Page.Xaml and the code is like below:

<ItemsControl x:Name="chatUI" VerticalAlignment="Bottom">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid x:Name="myGrid" Width="340" Background="{Binding Background}" HorizontalAlignment="{Binding GridHorizontalAlign}" Margin="10,0,10,10" MinHeight="45" BorderBrush="#FF003A4F" BorderThickness="0,0,0,2">
                <Polygon Visibility="{Binding RightVisibility}" Fill="{Binding Background}"  Points="0,0 5,6, 0,12" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,0,-5,0" />
                <Polygon Visibility="{Binding LeftVisibility}" Fill="{Binding Background}" Points="5,0 0,6, 5,12" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="-5,0,0,0" />
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding Text}" TextWrapping="Wrap" FontSize="15" FontFamily="Segoe UI" Foreground="White" Margin="10,10,10,0"/>
                    <TextBlock Grid.Row="1" Text="{Binding Time}" TextWrapping="Wrap" FontSize="11" FontFamily="Segoe UI" Foreground="LightGray" Margin="10,0,10,5" VerticalAlignment="Bottom" TextAlignment="Right"/>
                </Grid>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

What I need right now is getting Text which is bound to the TextBlock when I right click on the grid named myGrid. How is that possible in C#?


Solution

  • We can add the RightTapped event of the Grid, it will be fired when you right click the Grid.

    In the RightTapped event we can use Grid.Children to get the collection of child elements of the Grid. That we can get the Grid in the root Grid that named myGrid. That we can use the Grid.Children to get the TextBlock in the Grid.

    For example:

    private async void myGrid_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {
        var RightTapGrid = sender as Grid;
        var childernElements = RightTapGrid.Children;
        foreach (var item in childernElements)
        {
            var grid = item as Grid;
            if (grid != null)
            {
                var itemchildernElements = grid.Children;
                foreach (var text in itemchildernElements)
                {
                    var textBlock = text as TextBlock;
                    var dialog = new ContentDialog()
                    {
                        Title = textBlock.Text,
                        MaxWidth = this.ActualWidth 
                    };
                    dialog.PrimaryButtonText = "OK";
                    dialog.SecondaryButtonText = "Cancel";
                    await dialog.ShowAsync();
                    break;
                }
            }
        }
    }