按钮放在列表项上,如何在按钮事件处理程序中访问所述列表项?
Button placed on listitem, how to access said listitem in button eventhandler?
我正在 UWP 中制作一个播放列表管理应用程序,我需要能够对我设置的列表视图中的项目执行一些操作。我已将按钮附加到列表项,但我不知道如何在按钮的事件处理程序中引用 "parent" 列表项。
例如:
页面资源:
<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>
listview:(尽管它可能不相关。)
<ListView Name="PlayListView" ItemsSource="{x:Bind Playlists}"
IsItemClickEnabled="True"
ItemClick="PlayListView_ItemClick"
ItemTemplate="{StaticResource PlaylistDataTemplate}"
Visibility="Collapsed"
Height="420"
ScrollViewer.VerticalScrollBarVisibility="Visible"/>
以及删除按钮的事件处理程序。这就是我卡住的地方,我想引用单击按钮所在的列表项。
private void DeletePlayListButton_Click(object sender, RoutedEventArgs e)
{
?????
}
因此,如果您想要获得绑定的 JonPlaylist,如果您没有更改 DataTemplate 中的 DataContext(看起来您没有),您可以这样获得:
private void DeletePlayListButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
var boundItem = button.DataContext as JonPlaylist;
}
}
我正在 UWP 中制作一个播放列表管理应用程序,我需要能够对我设置的列表视图中的项目执行一些操作。我已将按钮附加到列表项,但我不知道如何在按钮的事件处理程序中引用 "parent" 列表项。
例如:
页面资源:
<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>
listview:(尽管它可能不相关。)
<ListView Name="PlayListView" ItemsSource="{x:Bind Playlists}"
IsItemClickEnabled="True"
ItemClick="PlayListView_ItemClick"
ItemTemplate="{StaticResource PlaylistDataTemplate}"
Visibility="Collapsed"
Height="420"
ScrollViewer.VerticalScrollBarVisibility="Visible"/>
以及删除按钮的事件处理程序。这就是我卡住的地方,我想引用单击按钮所在的列表项。
private void DeletePlayListButton_Click(object sender, RoutedEventArgs e)
{
?????
}
因此,如果您想要获得绑定的 JonPlaylist,如果您没有更改 DataTemplate 中的 DataContext(看起来您没有),您可以这样获得:
private void DeletePlayListButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
var boundItem = button.DataContext as JonPlaylist;
}
}