获取ItemsControl里面的控件
2016-06-28 19:03
106 查看
ItemsControl通过Binding获得数据,每个数据的模板在DataTemplate里面,直接查找DateTemplate里的控件获取不到。可以在ItemsControl的ItemContainerGenerator.StatusChanged时间里通过下列方法获得。
此处的itemsControl为ItemsControl的Name,FindSingleVisualChildren为自己写的搜索子控件的方法。
上面介绍的方法不支持ItemsControl虚拟化。
this.itemsControl.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged; private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e) { if (this.itemsControl.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) { this.itemsControl.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged; Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.DataBind, new Action(DelayedAction)); } } void DelayedAction() { for (int i = 0; i < this.itemsControl.Items.Count; i++) { var item = this.itemsControl.ItemContainerGenerator.ContainerFromIndex(i); StackPanel stackPanel = VisualTreeExHelper.FindSingleVisualChildren<StackPanel>(item); dateTimePicker.popupCalender1.IsOpen = false; } }
此处的itemsControl为ItemsControl的Name,FindSingleVisualChildren为自己写的搜索子控件的方法。
public static T FindSingleVisualChildren<T>(DependencyObject parentObj) where T : DependencyObject { T result = null; if (parentObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parentObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(parentObj, i); if (child != null && child is T) { result = child as T; break; } result = FindSingleVisualChildren<T>(child); if (result != null) break; } } return result; }
上面介绍的方法不支持ItemsControl虚拟化。
相关文章推荐
- flex 控件的重要属性
- 学习Winform文本类控件(Label、Button、TextBox)
- Delphi控件ListView的属性及使用方法详解
- web下载的ActiveX控件自动更新
- WinForm实现按名称递归查找控件的方法
- C#中父窗口和子窗口之间控件互操作实例
- C#编写ActiveX网页截图控件
- Android编程之Button控件用法实例分析
- Android控件之CheckBox、RadioButton用法实例分析
- 在Android开发中使用自定义组合控件的例子
- 一款超酷的Android自定义加载控件
- Android重写View实现全新的控件
- MFC中动态创建控件以及事件响应实现方法
- WinForm自定义函数FindControl实现按名称查找控件
- Android控件之ProgressBar用法实例分析
- WinForm拖拽控件生成副本的解决方法
- ASP.NET动态添加用户控件的方法
- ASP.NET的HtmlForm控件学习及Post与Get的区别概述
- 浅析Bootstrap验证控件的使用
- WinForm实现移除控件某个事件的方法