如何获取listbox中控件
来源:互联网 发布:文章千古事得失寸心知 编辑:程序博客网 时间:2024/05/16 11:32
1、创建一个类
public class FindListBoxControl
{
/// <summary>
/// 寻找一个指定元素
/// </summary>
/// <typeparam name="T"><peparam>
/// <param name="parent"></param>
/// <param name="childName"></param>
/// <returns></returns>
public T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
// Confirm parent and childName are valid.
if (parent == null) return null;
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
// If the child is not of the request child type child
T childType = child as T;
if (childType == null)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if (foundChild != null) break;
}
else if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
// If the child's name is set for search
if (frameworkElement != null && frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break;
}
}
else
{
// child element found.
foundChild = (T)child;
break;
}
}
return foundChild;
}
}
2、调用
private void lb_Service_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var index = lb_Service.SelectedIndex; //当前选中的item索引
ListBoxItem item = this.lb_Service.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;
TextBlock sp = new FindListBoxControl().FindChild<TextBlock>(item, "tb_Service");//根据当前 item 寻找stackpanel
PhoneCallTask call = new PhoneCallTask();
call.PhoneNumber = sp.Tag.ToString();
call.DisplayName = sp.Text.ToString();
call.Show();
}
- 如何获取listbox中控件
- WP7 ListBox中如何取得DateTemplate/ItemsPanelTemplate中的命名控件?
- WP7 ListBox中如何取得子控件中的内容?
- 如何在ListBox中添加控件后让ListBox滚动到最底部
- Javascriptym获取服务器端listbox控件的值
- 获取ListBox的ItemTemplate绑定的控件
- WP ListBox 遍历DataTemplate(获取所有控件)
- 获取指定文件夹文件名,并绑定ListBox控件显示在页面中
- MFC 通过主窗口向子窗口的listbox控件里赋值,并获取子窗口中listbox控件里的值
- WPF中ListBox控件的使用
- MFC中ListBox控件的使用
- MFC中Listbox控件的简单使用
- HTML中类似于ListBox的控件
- MFC中Listbox控件的简单使用
- MFC中ListBox控件的使用
- MFC中Listbox控件的使用
- MFC中Listbox控件的简单使用
- MFC中ListBox控件的简单使用
- 旧项目在iOS6中无法旋转的解决办法
- 杭电1064
- sharepoint 2010 自定义列表数据的增删改查常用操作 server object model
- 为什么Java程序占用的内存比实际分配给它的要多
- Mysql 高负载排查思路
- 如何获取listbox中控件
- 40位UUID, 及一个32位的不知是啥
- Sql server 2008 R2 Forget Password for Sa
- 万里航行总舵手——业务测试架构的设计
- mysql数据库基础操作
- 减少内存碎片
- Android数据库中查找一条数据 query方法详解
- 各类系统下载地址
- BootStrap入门教程 (一)