Reputation: 6590
I have created a grid dynamically which contents 6 rows. What I want to do is to create a Tap Event for each row dynamically. I am not getting how can I create a Tap event for each row.
Is there any examples ? or any solution ?
My view :
<Grid x:Name="LayoutRoot" Width="490" Background="{StaticResource PhoneChromeBrush}">
</Grid>
This is my grid. I have added column and rows dynamically. so how to create a Tap Event for row dynamically?
Upvotes: 0
Views: 2026
Reputation: 1
i somehow managed to do it Just had to set the background of the grid row to transparent. Credit to: Why can't I tap/click blank areas inside of a Border/ContentControl without setting the child's background to transparent?
Upvotes: 0
Reputation: 1739
Another solution to your problem would be to use a ListBox instead of a grid and use a custom template on the ListBoxItem.
Then when the user taps an item in the listbox you can capture which row they have selected by using the ListBox.SelectedItem / SelectedIndex property
Hope this helps
Grids are more for layout, not for interaction
Upvotes: 1
Reputation: 39027
You can't create a Tap event for a grid row. Put what you can do is putting all the controls of the row in a parent control, and create the event in that control.
For instance (in XAML, but you can do that in C# as well):
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<Grid.RowDefinitions>
<StackPanel Grid.Row="0" Tap="StackPanel_Tap">
<!-- Your controls here -->
</StackPanel>
<StackPanel Grid.Row="1" Tap="StackPanel_Tap">
<!-- Your controls here -->
</StackPanel>
<Grid>
Note that you can use a child grid instead of a stackpanel:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<Grid.RowDefinitions>
<Grid Grid.Row="0" Tap="GridRow_Tap">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!-- Your controls here -->
</Grid>
<Grid Grid.Row="0" Tap="GridRow_Tap">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<!-- Your controls here -->
</Grid>
<Grid>
In the C#, to assign an event to a control, use the += operator:
Control.Tap += Control_Tap;
Upvotes: 0