【问题标题】:How do I pass a List of values from one DataTemplate to another?如何将值列表从一个 DataTemplate 传递到另一个?
【发布时间】:2012-10-15 05:44:43
【问题描述】:

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

标签的 DataBinding 将是一个字符串列表 List&lt;String&gt; Tags

我的问题是我不确定如何引用第二个绑定,或者是否可以将任何内容的列表从一个 DataTemplate 传递到另一个。这可能吗?如果可以,怎么做?

【问题讨论】:

    标签: c# xaml windows-8 microsoft-metro


    【解决方案1】:

    如果您的标签集合是List&lt;String&gt;,那么在您的DateTemplate 中,DataContext 将是实际项目:所以给定的string 并且您可以使用以下语法绑定到当前的DataContext

    <DataTemplate x:Name="TagTemplate">
        <Border Background="LightGray">
            <TextBlock Text="{Binding}"/>
        </Border>
    </DataTemplate>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-10
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      相关资源
      最近更新 更多