获取ListBox的ItemTemplate绑定的控件
来源:互联网 发布:31岁转行做数据分析师 编辑:程序博客网 时间:2024/04/29 22:40
【转载 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a01eb1d5-cfc1-47fc-a8e5-45f7f02e8119】
How do I programmatically find a control inside an itemtemplates datatemplate, e.g. how do I find the checkbox inside the following XAML from the codebehind file..
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBoxx:Name="KontoFeltCheckBox"
Checked="KontoFeltCheckBox_Checked"
></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
You could walk the ListBoxItem Visual-Tree to find it’s ContentPresenter, and then find CheckBox from ContentPresenter’s ContentTemplate. Please see example below:
Markup:
<Window x:Class="FindControlInsideDataTemplate.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<ListBox Name="myListBox" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="KontoFeltCheckBox" Checked="KontoFeltCheckBox_Checked">
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Test" Click="Button_Click"/>
</StackPanel>
</Window>
Code:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace FindControlInsideDataTemplate
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new List<Person>()
{
new Person(){Name = "Tom"},
new Person(){Name = "Ken"},
new Person(){Name = "Peter"}
};
}
private void KontoFeltCheckBox_Checked(object sender, RoutedEventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromIndex(0));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
CheckBox target = (CheckBox)myDataTemplate.FindName("KontoFeltCheckBox", myContentPresenter);
}
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
}
public class Person
{
public string Name { set; get; }
}
}
- 获取ListBox的ItemTemplate绑定的控件
- ListBox控件的数据绑定
- ListBox控件的数据绑定
- WPF 自定义listbox的ItemTemplate实现通讯录
- Silverlight 中 ListBox.ItemTemplate 如何绑定 Tag
- 数据绑定到DataList中ItemTemplate的时候,对里面控件的处理
- GridView1_RowDataBound中能不能得到ItemTemplate中Label控件绑定的Text值??
- DataList控件与ItemTemplate的用法
- Javascriptym获取服务器端listbox控件的值
- ListBox的数据绑定
- ListBox的数据库绑定
- ListBox绑定的研究
- ListBox控件绑定数据库的两种方法
- GridView中itemTemplate绑定字段的四种方式
- GridView中itemTemplate绑定字段的几种方式
- GridView中itemTemplate绑定字段的几种方式
- ListBox控件的使用
- ListBox控件的使用
- 有符号整数加减溢出检测问题
- spring动态代理基本原理
- 和SIP有关的RFC
- linux wine安装与使用
- 自己写的一个主键生成器
- 获取ListBox的ItemTemplate绑定的控件
- 指针加减操作,当a为数组时a和&a的区别
- 从下拉菜单设计细节看Amazon对用户体验的把握,商品分类下拉菜单
- 选择外汇交易品种很重要
- TPMC
- javascript去除字符串两端的空格
- java实现文件上传,文件下载功能 源码
- php抓取页面方法汇总
- DEVCAPS1