Reputation: 15
how can I get to the contents of the clicked grid Item in a grid view ... for example the grid item has a textbox ... how could I get the contents of this textbox when the gridview Item is clicked vie ItemClickEventArgs ? thanks
Upvotes: 0
Views: 4741
Reputation: 11
If you are using a DataTemplate to display the items in the GridView, I have found the simplest way is to capture Tapped, Hold, etc.. event on the DataTemplate's control that holds all the item.
In the event you can code
private void ItemTapped(object sender, TappedRoutedEventArgs e)
{
var clickedItem = (sender as FrameworkElement).DataContext;
}
That will give you the object within the item that was clicked.
Upvotes: 0
Reputation: 1441
Assuming you're using ItemsSource with your GridView, you can use e.ClickedItem to get the DataContext of the GridViewItem you clicked. If you want the actual GridViewItem you can use
GridViewItem item = (sender as GridView).ItemContainerGenerator.ContainerFromItem(e.ClickedItem) as GridViewItem;
Then you can use VisualTreeHelper.GetChild to dig down as necessary (like to find a TextBox).
Upvotes: 0
Reputation: 15296
Try this code.
<GridView x:Name="gv" SelectionChanged="gvSelectionChanged">
<GridViewItem>
<TextBox x:Name="txtOne" />
</GridViewItem>
<GridViewItem>
<TextBox x:Name="txtTwo" />
</GridViewItem>
</GridView>
private void gvSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var SelectedText = ((TextBox)((GridViewItem)gv.SelectedItem).Content).Text;
}
Upvotes: 0