C#中如何获取ItemsControl的DataTemplate中定义的控件?
2012-08-07 10:37
701 查看
开发环境:VisualStudio2010 +Silverlight4
新建一个test.xaml,并添加ComboBox组合框控件,其Xaml代码如下:
那么我们如何获取DataTemplate中的名为txtID的TextBox控件或者其它控件呢?
在test.xaml.cs文件中不能够直接引用名为txtID的TextBox控件,即this.txtID是不能访问的,因为txtID只属于DataTemplate模板的名称空间范围内(类似于参数的作用域),可参看Silverlight4文档的TemplatePartAtrribute。我们如何获得DataTemplate的TextBox控件呢?可用如下方式获得:
方法一:
方法二:
对于
TextBox txtBox = (TextBox)VisualTreeHelper.GetChild(this.cmbTemplate.LoadContent(), 0);
因为它的Text的值是绑定数据源的ID属性(如:{Binding Path=ID}),通过程序运行动态生成的值,因此通过这两种方式获得的TextBox的Text是空值,而对于tbCategoryName名的TextBlock,因为它的Text值是固定的“Name”字符串,因此通过这两种方法获得的TextBlock的Text值为“Name”。
如果我们想获得选中的ComboBoxItem的TextBox的值或者Image的Source值,则可通过如下方式获得:
可能有更好的方法获得绑定数据源时动态生成的值,本人不才,还未想出该方法,可讲究用上面的方法获得,如果找到了相应方法就做更新。
原出处:http://blog.csdn.net/wackelbh/article/details/6003947#comments
新建一个test.xaml,并添加ComboBox组合框控件,其Xaml代码如下:
[xhtml] view plaincopy<ComboBox x:Name="cmbBoxCategory" Height="28" Margin="105,69,182,0" VerticalAlignment="Top" SelectionChanged="cmbBoxCategory_SelectionChanged" DataContext="{StaticResource SharingObjectCategoryDataSource}" ItemsSource="{Binding CategoryCollection }" > <ComboBox.ItemTemplate> <DataTemplate x:Name="cmbTemplate"> <StackPanel Orientation="Horizontal"> <TextBox x:Name="txtID" Text="{Binding Path=ID}"/> <Image Source="{Binding Path=ImagePath}"/> <TextBlock x:Name="tbCategoryName" Text="Name"/> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
那么我们如何获取DataTemplate中的名为txtID的TextBox控件或者其它控件呢?
在test.xaml.cs文件中不能够直接引用名为txtID的TextBox控件,即this.txtID是不能访问的,因为txtID只属于DataTemplate模板的名称空间范围内(类似于参数的作用域),可参看Silverlight4文档的TemplatePartAtrribute。我们如何获得DataTemplate的TextBox控件呢?可用如下方式获得:
方法一:
[c-sharp] view plaincopyTextBox txtBox = (TextBox)VisualTreeHelper.GetChild(this.cmbTemplate.LoadContent(), 0); // this.cmbTemplate.LoadContent()是获取cmbTemplate名的DataTemplate的内容,该例中获 //得的是StackPanel,再通过VisualTreeHelper.GetChild()方法获得属于该StackPanel的控件, //可参看Silverlight4文档的LoadContent()方法的使用
方法二:
[c-sharp] view plaincopyStackPanel panel = (StackPanel)this.cmbTemplate.LoadContent(); TextBlock tbCategory=panel.FindName("tbCategoryName") as TextBlock; //可参看Silverlight4文档的FindName()方法的使用
对于
TextBox txtBox = (TextBox)VisualTreeHelper.GetChild(this.cmbTemplate.LoadContent(), 0);
因为它的Text的值是绑定数据源的ID属性(如:{Binding Path=ID}),通过程序运行动态生成的值,因此通过这两种方式获得的TextBox的Text是空值,而对于tbCategoryName名的TextBlock,因为它的Text值是固定的“Name”字符串,因此通过这两种方法获得的TextBlock的Text值为“Name”。
如果我们想获得选中的ComboBoxItem的TextBox的值或者Image的Source值,则可通过如下方式获得:
[c-sharp] view plaincopy//test.xaml.cs代码 private void cmbBoxCategory_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (this.cmbBoxCategory.SelectedItem!=null) { ComboBox item = sender as ComboBox; Category cat = (Category)this.cmbBoxCategory.SelectedItem; //Category类为cmbBoxCategory组合框绑定的数据源类,然后再通过cat获取该类中的ImagePath属性即可 this.imgObject.Source = new BitmapImage(new Uri(cat.ImagePath, UriKind.Relative)); //imgObject为test.xaml中的一个Image控件 } }
可能有更好的方法获得绑定数据源时动态生成的值,本人不才,还未想出该方法,可讲究用上面的方法获得,如果找到了相应方法就做更新。
原出处:http://blog.csdn.net/wackelbh/article/details/6003947#comments
相关文章推荐
- [Silverlight学习笔记]如何获取ItemsControl的DataTemplate中定义的控件?
- [Silverlight学习笔记]如何获取ItemsControl的DataTemplate中定义的控件?
- 如何获取GridView的EmptyDataTemplate中的控件
- Windows Phone 如何获取ListBox的ItemTemplate/DataTemplate 中指定的控件
- Silverlight实用窍门系列:66.Silverlight的数据模板DataTemplate(二)获取数据模板控件
- Silverlight实用窍门系列:66.Silverlight的数据模板DataTemplate(二)获取数据模板控件
- .net c# 中获得GridView的EmptyDataTemplate中的控件的方法
- .net c# 中获得GridView的EmptyDataTemplate中的控件的方法
- WPF 遍历DataTemplate(获取所有控件)
- 如何在后台代码获取控制EditItemTemplate的控件
- C# 控件如何获取事件已经注册的方法
- 在WPF中获取DataGridTemplateColumn模板定义的内容控件
- WPF silverlight获取子控件(获取DataTemplate里的子控件)
- WP ListBox 遍历DataTemplate(获取所有控件)
- WPF 遍历DataTemplate(获取所有控件)
- 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
- C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值
- 在WPF中获取DataGridTemplateColumn模板定义的内容控件
- DataGridTemplateColumn 如何获取内部控件
- WPF中如何使用C#创建DataTemplate数据模版