1
votes

I am working on a Win8 Metro Application. I am using a GridView. In GridView's ItemTemplate, I have 3 buttons (Button1, Button2, Button3). So, on my screen, if I have 5 GridView Items at a time, then I will see 5x3 = 15 buttons.

The problem is that if any of these buttons is clicked, I am unable to trace parent GridView Item.

Amongst GridView's properties, "SelectedIndex" (or SelectedItem) properties are there. However, its value is set when GridView item is clicked, (not when a button inside GridViewItem is clicked). So, on clicking button, SelectedIndex remains -1.

How can I find out on ButtonClick that who is the parent item of this button?

2
Do you want the grid view item, or the data item bound to it? - ZombieSheep
Maybe yes. I want to refer to ID of grid view item in my button's click code. - shuaibpk

2 Answers

1
votes

From reading your replies in comments, it seems like you want to get the reference to the underlying data item rather than the grid item itself. That being the case, do the following in your button click handler...

Void Button_Click(sender, e)
{
    var btn = (Button)sender;
    var dc = btn.DataContext as MyBoundType;  // don't forget to check for null!
    var itemId = dc.Id; // assuming your object has an Id field
}
0
votes

Bind the CommandParameter of the button to the parent item like this:

<Button 
    Command="{Binding RemoveCommand}"
    CommandParameter="{Binding RelativeSource=
        {RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
    />