1 回答
TA贡献1862条经验 获得超6个赞
让我们考虑您的项目是一个类的对象LineItem。触发按钮单击后,您可以使用senderfrom click 事件参数来获取DataContext相同的对象。
(作为 FrameworkElement 的发送者).DataContext
//小样本
主页.Xaml
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="YourPage"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<GridView x:Name="GridView">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:LineItem">
<StackPanel>
<TextBlock Text="{x:Bind Title}"></TextBlock>
<TextBlock Text="{x:Bind description}"></TextBlock>
<Button Content="Action" Click="Button_Click"></Button>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
</Page>
主页.Xaml.cs
public sealed partial class MainPage : Page
{
List<LineItem> MyDictionary = new List<LineItem>();
public MainPage()
{
MyDictionary.Add(new LineItem() { Title = "Item1", description = "Desc1" });
MyDictionary.Add(new LineItem() { Title = "Item2", description = "Desc2" });
MyDictionary.Add(new LineItem() { Title = "Item3", description = "Desc3" });
this.InitializeComponent();
GridView.ItemsSource = MyDictionary;
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
LineItem clicked_item =(LineItem)((sender as FrameworkElement).DataContext);
await (new MessageDialog(clicked_item.Title + " Button is clicked ")).ShowAsync();
}
}
public class LineItem
{
public string Title { get; set; }
public string description { get; set; }
}
- 1 回答
- 0 关注
- 125 浏览
添加回答
举报