I’d like to get the ListViewItem of a button. In normal WPF, I’d use GetContainerForItem() but can’t find the equivalent for WinRT. In my AddButton_Click() function, you will see the brittle way that I get the id of the item that the associated button was pushed.
<DataTemplate x:Key="Custom80ItemTemplate">
<Grid Margin="6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Width="40" Height="40" Click="AddButton_Click" Content="" FontFamily="Segoe UI Symbol" Style="{StaticResource TextButtonStyle}" />
<StackPanel Grid.Column="1" Margin="10,0,0,0">
<TextBlock Text="{Binding name}" Style="{StaticResource ItemTextStyle}" MaxHeight="40"/>
<TextBlock Text="{Binding id}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/>
<TextBlock Text="{Binding group}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/>
</StackPanel>
</Grid>
</DataTemplate>
<ListView x:Name="searchResultsListView" Grid.Row="1"
SelectionMode="None"
HorizontalAlignment="Left"
Margin="10,0,10,10"
ItemTemplate="{StaticResource Custom80ItemTemplate}" />
private async void AddButton_Click(object sender, RoutedEventArgs e)
{
var parent1 = ((FrameworkElement)sender).Parent;
var grid = parent1 as Grid;
var stackpanel = grid.Children[1] as StackPanel;
var textBlock = stackpanel.Children[1] as TextBlock;
}
Why do you need to access the elements in the template directly? If you only want to access
idvalue of the clicked item, you could just put the command for the button in the item itself:You can find a
RelayCommandimplementation here.Now just replace the event handler by binding
command: