C# 按钮放置在listitem上,如何访问按钮eventhandler中的listitem?
我正在UWP中制作一个播放列表管理应用程序,我需要能够对我设置的列表视图中的项目执行许多操作。我已将按钮附加到listitems,但我不知道如何在按钮的eventhandler中引用“父”listitem 例如: 页面资源:C# 按钮放置在listitem上,如何访问按钮eventhandler中的listitem?,c#,asp.net,xaml,uwp,universal,C#,Asp.net,Xaml,Uwp,Universal,我正在UWP中制作一个播放列表管理应用程序,我需要能够对我设置的列表视图中的项目执行许多操作。我已将按钮附加到listitems,但我不知道如何在按钮的eventhandler中引用“父”listitem 例如: 页面资源: <DataTemplate x:DataType="data:JonPlaylist" x:Key="PlaylistDataTemplate"> <StackPanel> <StackPanel Ori
<DataTemplate x:DataType="data:JonPlaylist" x:Key="PlaylistDataTemplate">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name:" Foreground="Azure" VerticalAlignment="Bottom" HorizontalAlignment="Left" FontStyle="Italic" Margin="0,0,10,0"/>
<Button Name="DeletePlayListButton" Content="Delete" Foreground="Azure" Background="SteelBlue" BorderBrush="Azure" Click="DeletePlayListButton_Click"/>
</StackPanel>
</StackPanel>
</DataTemplate>
因此,如果您希望获得绑定的播放列表,如果您没有更改DataTemplate中的DataContext(看起来您没有),您可以得到如下结果:
private void DeletePlayListButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
var boundItem = button.DataContext as JonPlaylist;
}
}
您希望获取绑定项还是可视化树中的项,即SelectorItem?
private void DeletePlayListButton_Click(object sender, RoutedEventArgs e)
{
?????
}
private void DeletePlayListButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
var boundItem = button.DataContext as JonPlaylist;
}
}