请问关于c的具体应用场景有哪些?
- 内容介绍
- 文章标签
- 相关推荐
本文共计241个文字,预计阅读时间需要1分钟。
您有一个列表框,其中包含一个包含按钮的数据模型。当单击按钮时,您想进入按钮的click handler,并获取当前列表框项的索引。以下是一个简化的解决方案:
csharpprivate void Button_Click(object sender, EventArgs e){ Button button=sender as Button; if (button !=null) { // 获取按钮所在的列表框项 ListBox listBox=button.Parent as ListBox; if (listBox !=null) { // 获取按钮在列表框中的索引 int index=listBox.Items.IndexOf(button); // 输出索引或进行其他操作 Console.WriteLine(Button index: + index); } }}
我有一个列表框,其中包含一个包含按钮的数据模板.单击按钮时,我想进入按钮
click handler当前列表框项的索引?
我该怎么办?
马尔科姆
更合适的答案,private void Button_Click(object sender, RoutedEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is ListViewItem)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) return; int index = lstBox.ItemContainerGenerator.IndexFromContainer(dep); }
本文共计241个文字,预计阅读时间需要1分钟。
您有一个列表框,其中包含一个包含按钮的数据模型。当单击按钮时,您想进入按钮的click handler,并获取当前列表框项的索引。以下是一个简化的解决方案:
csharpprivate void Button_Click(object sender, EventArgs e){ Button button=sender as Button; if (button !=null) { // 获取按钮所在的列表框项 ListBox listBox=button.Parent as ListBox; if (listBox !=null) { // 获取按钮在列表框中的索引 int index=listBox.Items.IndexOf(button); // 输出索引或进行其他操作 Console.WriteLine(Button index: + index); } }}
我有一个列表框,其中包含一个包含按钮的数据模板.单击按钮时,我想进入按钮
click handler当前列表框项的索引?
我该怎么办?
马尔科姆
更合适的答案,private void Button_Click(object sender, RoutedEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is ListViewItem)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) return; int index = lstBox.ItemContainerGenerator.IndexFromContainer(dep); }

