更改DataTemplate中存在的TextBlock文本 [英] Change the TextBlock Text present in a DataTemplate
本文介绍了更改DataTemplate中存在的TextBlock文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataTemplate,其中我有一个带有2个RowDefinitions的Grid布局。我在第一行有一个TextBox,在第二行有一个ComboBox。
我在ResourceDictionary中定义了DataTemplate。
这是DataTemplate的代码:
< DataTemplate x:Key = myDataTemplate >
< 网格 >
< ; Grid.RowDefinitions >
< RowDefinition / >
< RowDefinition / >
< / Grid.RowDefinitions >
< TextBox 名称 = txtChannelDescription Grid.Row = 0 保证金 = 1,1,1,1 / > ;
< ComboBox 名称 = < span class =code-keyword> cmbChannelTag Grid.Row = 1 IsReadOnly = True 保证金 = 1,1,1, 1 / >
< / Grid >
< / DataTemplate >
I我正在使用代码后面的DataTemplate是:
(DataTemplate)FindResource(myDataTemplate)
如何在运行时设置TextBox.Text的值和ComboBox的ItemSource?
解决方案
嘿Abhishek,看看我做了什么,
1)首先,我在Xaml中使用了一个ListBox,并像这样分配了它的ItemTemplate属性 -
< listbox name =ItemListitemtemplate ={StaticResource myDataTemplate}/>
2)在代码背后我编写了类似这样的代码 -
列表< string> Items = new List< string>();
public MainWindow()
{
InitializeComponent();
Items.Add( Abc);
ItemList.ItemsSource = Items;
ItemList.SelectionChanged + = ItemList_SelectionChanged;
}
void ItemList_SelectionChanged( object sender ,SelectionChangedEventArgs e)
{
ListBoxItem myListBoxItem =(ListBoxItem)(ItemList.ItemContainerGenerator.ContainerFromItem(ItemList.SelectedItem));
ContentPresenter myContentPresenter = FindVisualChild< contentpresenter>(myListBoxItem);
// 从ContentPresenter上设置的DataTemplate中查找textBlock
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBox myTextBlock =(TextBox)myDataTemplate.FindName( txtChannelDescription,myContentPresenter);
if (myTextBlock!= null )
{
myTextBlock.Text = 在此处输入文字;
}
}
私人 childItem FindVisualChild< childitem>(DependencyObject obj)其中 childItem:DependencyObject
{
for ( int i = 0 ; i < VisualTreeHelper.GetChildrenCount(obj); i ++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj,i);
if (child!= null && child 是 childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild< childitem>(child);
if (childOfChild!= null )
返回 childOfChild;
}
}
返回 null ;
}
所以通过这种方式使用datatemplate的FindNameAPI,你可以找到孩子组件>
I have a DataTemplate in which I have a Grid layout with 2 RowDefinitions. I have a TextBox in the first row and a ComboBox in the second row.
I have defined the DataTemplate in my ResourceDictionary.
This is the code for the DataTemplate:
<DataTemplate x:Key="myDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name ="txtChannelDescription" Grid.Row="0" Margin="1,1,1,1"/>
<ComboBox Name="cmbChannelTag" Grid.Row="1" IsReadOnly="True" Margin="1,1,1,1"/>
</Grid>
</DataTemplate>
I am using this DataTemplate in code behind as:
(DataTemplate)FindResource("myDataTemplate")
How do I set the Value of TextBox.Text and the ItemSource of the ComboBox at runtime?
解决方案
Hey Abhishek, Just look at here what I did,
1) First of all I took one ListBox in Xaml and assign its ItemTemplate property like this -
<listbox name="ItemList" itemtemplate="{StaticResource myDataTemplate}" />
2)In code behind I wrote code something like this-
List<string> Items = new List<string>(); public MainWindow() { InitializeComponent(); Items.Add("Abc"); ItemList.ItemsSource = Items; ItemList.SelectionChanged += ItemList_SelectionChanged; } void ItemList_SelectionChanged(object sender, SelectionChangedEventArgs e) { ListBoxItem myListBoxItem = (ListBoxItem)(ItemList.ItemContainerGenerator.ContainerFromItem(ItemList.SelectedItem)); ContentPresenter myContentPresenter = FindVisualChild<contentpresenter>(myListBoxItem); // Finding textBlock from the DataTemplate that is set on that ContentPresenter DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; TextBox myTextBlock = (TextBox)myDataTemplate.FindName("txtChannelDescription", myContentPresenter); if(myTextBlock != null) { myTextBlock.Text = "Enter Your Text Here"; } } 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; }
So In this way by using "FindName" API of datatemplate, you can find the child element>
这篇关于更改DataTemplate中存在的TextBlock文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文