如何识别gridview中哪个项目中的元素

我使用“gridview”不仅可以“显示”,还可以让用户与之交互,例如单击特定项目内的按钮。假设我的gridview是这样的:


<Gridview>

  <Gridview.Itemtemplate>

    <DataTemplate>

      <StackPanel>

        <Image> </Image>

        <Button> </Button>

      </StackPanel>

   </DataTemplate>

</Gridview.Itemtemplate>

如果我按下按钮,gridview 中所有项目的每个按钮都会做同样的事情。我们如何按下按钮并知道它属于哪个项目?


*注意:仅按下按钮不会触发“ItemIsSelected”事件,因为该项目没有被点击,它是按钮。


倚天杖
浏览 126回答 1
1回答

阿波罗的战车

让我们考虑您的项目是一个类的对象LineItem。触发按钮单击后,您可以使用senderfrom click 事件参数来获取DataContext相同的对象。(作为 FrameworkElement 的发送者).DataContext//小样本主页.Xaml<Page&nbsp; &nbsp; x:Class="App1.MainPage"&nbsp; &nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&nbsp; &nbsp; xmlns:local="using:App1"&nbsp; &nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&nbsp; &nbsp; xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&nbsp; &nbsp; mc:Ignorable="d"&nbsp;&nbsp; &nbsp; x:Name="YourPage"&nbsp; &nbsp; Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">&nbsp; &nbsp; <Grid>&nbsp; &nbsp; &nbsp; &nbsp; <GridView x:Name="GridView">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <GridView.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate x:DataType="local:LineItem">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Text="{x:Bind Title}"></TextBlock>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Text="{x:Bind&nbsp; description}"></TextBlock>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Button Content="Action" Click="Button_Click"></Button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </GridView.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; </GridView>&nbsp; &nbsp; </Grid></Page>主页.Xaml.cspublic sealed partial class MainPage : Page&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<LineItem> MyDictionary = new List<LineItem>();&nbsp; &nbsp; &nbsp; &nbsp; public MainPage()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyDictionary.Add(new LineItem() { Title = "Item1", description = "Desc1" });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyDictionary.Add(new LineItem() { Title = "Item2", description = "Desc2" });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyDictionary.Add(new LineItem() { Title = "Item3", description = "Desc3" });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GridView.ItemsSource = MyDictionary;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private async void Button_Click(object sender, RoutedEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LineItem clicked_item&nbsp; =(LineItem)((sender as FrameworkElement).DataContext);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await (new MessageDialog(clicked_item.Title + " Button is clicked ")).ShowAsync();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class LineItem&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Title { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string description { get; set; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP