首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

请教怎么实现 点击ListBox的项中的Button,获取Model中的属性

2012-04-10 
请问如何实现 点击ListBox的项中的Button,获取Model中的属性?先上代码:XAML:XML codeListBox x:Namelis

请问如何实现 点击ListBox的项中的Button,获取Model中的属性?
先上代码:
XAML:

XML code
<ListBox x:Name="listBox">                <ListBox.ItemTemplate>                    <DataTemplate>                        <StackPanel Margin="12">                            <TextBlock Text="{Binding Name,StringFormat=姓名:\{0\}}"                                       Foreground="Orange"/>                            <TextBlock Text="{Binding Age,StringFormat=年龄:\{0\}}"                                       Foreground="Gray"/>                            <Button Content="获取学生ID"/>                        </StackPanel>                    </DataTemplate>   </ListBox.ItemTemplate></ListBox>


C#:
C# code
//modelpublic class Student{      public string Name { get; set; }      public int Age { get; set; }      public int ID { get; set; }}            List<Student> students = new List<Student>             {                new Student{Name = "st1",Age = 20,ID = 1},                new Student{Name = "st2",Age = 18,ID = 2},                new Student{Name = "st3",Age = 21,ID = 3},                new Student{Name = "st4",Age = 24,ID = 4},                new Student{Name = "st5",Age = 25,ID = 5},                new Student{Name = "st6",Age = 26,ID = 6},                new Student{Name = "st7",Age = 27,ID = 7},                new Student{Name = "st8",Age = 28,ID = 8},                new Student{Name = "st9",Age = 29,ID = 9},            };            listBox.ItemsSource = students;



描述:
ListBox中的每一项都是一个Student对象,显示Name和Age, 点击ListBoxItem中的Button时候,我想获取到该学生的ID,请问我应该怎么做?


[解决办法]
((Student)(((System.Windows.FrameworkElement)(((System.Windows.FrameworkElement)(((Button)sender))).Parent)).DataContext)).ID

热点排行