windows store app如何从数据绑定的usercontroles中获取值

时间:2013-10-10 05:46:09

标签: windows-8 windows-runtime microsoft-metro windows-store-apps winrt-xaml

如何从数据绑定视图元素中获取值 我使用列表视图和数据绑定到集合

<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事件中获取值

1 个答案:

答案 0 :(得分: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;
    }
}