Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
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?
Posted

1 solution

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-

C#
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>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900