c# - 如何将值列表从一个 DataTemplate 传递到另一个 DataTemplate?

标签 c# xaml windows-8 microsoft-metro

我有一个字符串值(或标签)列表,我想在另一个 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/

相关文章:

c# - 启动 Storyboard时未设置目标错误

silverlight - 在 Silverlight/WinRT 中仅使用 XAML 将元素定位在 Canvas 内的中心(而不是左上角)

c# - 像c#中的Lists一样用括号初始化元素

c# - 无法为 x64 和 x86 加载文件或程序集 'CefSharp.Wpf;只有一个作品

c# - 异步网络操作永远不会完成

windows-8 - 使用 Proximity API 与许多 Windows/Windows Phone 8 设备同时通信

xaml - 如何制作可执行的 Metro 风格应用程序?

c# - REST 错误 - 请求包含实体主体但没有内容类型 header 。推断的媒体类型应用程序/八位字节流不支持此资源

wpf - 响应 View 模型中的路由命令

javascript - 快照 View windows 8 javascript