WPF DataTemplate Textblock绑定 [英] WPF DataTemplate Textblock binding
问题描述
我有一个学生列表框和该列表项的数据模板. DataTemplate具有一个名为tb的文本块. 我想将此文本块设置为绑定到Name属性. 我该如何在外部以xaml格式进行操作? (不是来自datatemplate)
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>
谢谢
推荐答案
如果我正确理解您的意思,您是在问如何设置文本块(当前位于DataTemplate中)的数据绑定?您不能在ListBox级别设置该数据绑定.它必须在您的DataTemplate中完成.
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.
在这种情况下,DataTemplate将继承列表中每个项目的DataContext.
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>
换句话说-此DataTemplate是列表中每个项目的模板-而DataContext将是列表中每个项目.
In other words - this DataTemplate is a template for each item in the list - and the DataContext will be each item in the list.
这篇关于WPF DataTemplate Textblock绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!