我想获得一个按钮的 ListViewItem 。在普通 WPF 中,我会使用 GetContainerForItem() 但找不到 WinRT 的等价物。在我的 AddButton_Click() 函数中,您将看到我获取相关按钮被按下的项目 id 的脆弱方式。
<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;
}