1
votes

I have a listbox of Students and the datatemplate for list item. The DataTemplate has a text block named tb. I want to set this textblock to be binded to Name property. How can I do it in xaml form outside? (Not from the datatemplate)

<ListBox ItemsSource="{Binding l}"      ItemTemplate="{Binding DataTemplate_L}" Margin="12,70,0,0">

</ListBox>

Thank you

1

1 Answers

0
votes

If I understand you correctly, you're asking how you set the databinding for the textblock, that is currently in your DataTemplate? You can't set that databinding at the ListBox level; it has to be done in your DataTemplate.

In this case the DataTemplate will inherit the DataContext of each item in the list.

<DataTemplate x:Key="myDataTemplate">
    <StackPanel>
       <TextBlock Text="{Binding Path=Name}" />
       <TextBlock Text="{Binding Path=AnotherListItemProperty}" />
    </StackPanel>
</DataTemplate>

In other words - this DataTemplate is a template for each item in the list - and the DataContext will be each item in the list.