我有一个字符串值(或标签)列表,我想在另一个 DataTemplate 中创建一个 DataTemplate。例如,假设我有一个包含字符串、整数和字符串列表的对象。字符串列表是我感兴趣的标签集。对于每个标签,我都有一个要使用的特定 DataTemplate:
<!-- This is the Tag Template-->
<DataTemplate x:Name="TagTemplate">
<Border Background="LightGray">
<TextBlock Text="{Binding TagValue}"/> <!-- This is where I'm not sure how to reference the individual tag-->
</Border>
</DataTemplate>
另一个 DataTemplate 的主体将包含如下标签:
<!-- This is the main Data Template for the overall data-->
<DataTemplate>
<Grid>
<GridView ItemsSource="{Binding Tags}" ItemTemplate="{StaticResource TagTemplate }"/>
<!-- Below is a commented static representation of the tags-->
<!--<TextBlock Text="TAG, TAG, TAG, TAG, TAG" Margin="5, 5, 5, 5"/>-->
</Grid>
<DataTemplate>
标签的数据绑定(bind)将是字符串列表 List<String> Tags
我的问题是我不确定如何引用第二个绑定(bind),或者是否可以从 DataTemplate
传递任何内容的列表。到另一个。这可能吗?如果可能的话,如何实现?
最佳答案
如果您的标签集合是 List<String>
然后在你的 DateTemplate
DataContext
将是实际的项目:所以给定的 string
您可以绑定(bind)到当前的 DataContext
使用以下语法:
<DataTemplate x:Name="TagTemplate">
<Border Background="LightGray">
<TextBlock Text="{Binding}"/>
</Border>
</DataTemplate>
关于c# - 如何将值列表从一个 DataTemplate 传递到另一个 DataTemplate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12889912/