I create a Grid, each element of which has a couple of buttons, and I want to make various manipulations by clicking on one of the buttons. Here is the xaml:

<GridView Name="list" IsItemClickEnabled="True" Margin="0,90,0,0"> <GridView.ItemTemplate> <DataTemplate > <StackPanel Margin="10"> <Image Width="200" Height="200" Source="{Binding ImagePath}" /> <Grid Background="#7F9BD1D4" Height="Auto" > <Grid.ColumnDefinitions> <ColumnDefinition Width="1*" /> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <Button Name="Car" Grid.Column="0" Content="Машина" HorizontalAlignment="Stretch" Background="#7F9BD1D4" Foreground="White" Click="Button_Click"/> <Button Name="Cat" Grid.Column="1" Content="Кошка" HorizontalAlignment="Stretch" IsEnabled="{Binding Act}" Background="#7F9BD1D4" Foreground="White"/> </Grid> <TextBlock FontSize="16" Text="{Binding Title}" HorizontalAlignment="Center"/> </StackPanel> </DataTemplate> </GridView.ItemTemplate> </GridView> 

Here is the great code

 private void Button_Click(object sender, RoutedEventArgs e) { Car.Visibility = Visibility.Collapsed; } 

And, actually, the problem ... Mistake

  • one
    And if instead of Car use ((Button)sender) ? - VladD
  • ............ I just wrote sender ... the button didn't write ... Thank you) - Denisok 4:42 pm
  • Okay, then post it as an answer. - VladD
  • @VladD Please tell me, I need to click on the second button on my pop-up window to display the image of this element GridView ( <Image Width="200" Height="200" Source="{Binding ImagePath}" /> ) I can not to figure out how to display an image with an item in which the button is located ... - Denisok
  • Immediately I do not think, it is not obvious. I think it is worth asking a separate question. - VladD 7:57 pm

1 answer 1

The fact is that names like Car are visible in the C # code only for elements that are added at the compilation stage. There are no names for dynamically defined patterns (for example, because a pattern can be applied several times).

The easiest way is that your sender will be essentially the element that clicked on. That is, you just need to write

 ((Button)sender).Visibility = Visibility.Collapsed;