0

我如何从数据绑定器视图元素中获取值正在使用列表视图并将数据绑定到集合

<ListView.Resources>
            <DataTemplate x:Key="DataTemplate1">
                <Grid Height="20" Width="100" Background="#FFF5F3F3" Tapped="Grid_Tapped">
                    <TextBlock Text="{Binding Name}" Foreground="#FF0E0303"/>
                    <TextBlock Text="{Binding Age}" Foreground="#FF0E0303"/>
                </Grid>
            </DataTemplate>

现在我想要的是在 gridtapped 事件中取回值

4

1 回答 1

1

尝试这个

<ListView x:Name="lv" IsItemClickEnabled="True" ItemClick="lv_ItemClick_1">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Height="20" Width="100" Background="#FFF5F3F3">
                <TextBlock Text="{Binding Name}" Foreground="#FF0E0303"/>
                <TextBlock Text="{Binding Age}" Foreground="#FF0E0303"/>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    lv.ItemsSource = new List<Person> 
    {
        new Person("Charles", 25),
        new Person("Mark", 27),
        new Person("John", 22),
    };
}

private void lv_ItemClick_1(object sender, ItemClickEventArgs e)
{
    var objPerson = (Person)e.ClickedItem;
}

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}
于 2013-10-10T07:24:06.740 回答