XAML错误:“属性'VisualTree'设置不止一次”

时间:2014-04-09 14:18:21

标签: xaml winrt-xaml

我正在尝试将两个网格放在DataTemplate中。

我的代码如下所示,出现以下错误。

  

错误:“属性'VisualTree'设置了多次”

<DataTemplate x:Key="PareoItemTemplate">
    <Grid x:Name="gridColorEjercicio" Height="100" Width="350" Background="#FFF0F0F0" Margin="-11,0,0,0">
        <StackPanel Margin="0" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
            <StackPanel Margin="0,10,15,0" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="{Binding letter}" FontSize="24" FontFamily="Resources/Fonts/Programa Tutorias Bold.ttf#Programa Tutorias" Foreground="Black" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap" Text="{Binding option}" FontSize="24" FontFamily="Resources/Fonts/Programa Tutorias Bold.ttf#Programa Tutorias" Width="253" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </StackPanel>
            <Grid VerticalAlignment="Center" Margin="5,10,5,0" HorizontalAlignment="Center">
                <Image Source="{Binding imageURI}" />
            </Grid>
        </StackPanel>
    </Grid>
    <Grid x:Name="gridPareoColorEjercicio" Height="100" Width="350" Background="#FFF0F0F0" Margin="-11,0,0,0">
        <StackPanel Margin="0" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
            <StackPanel Margin="0,10,15,0" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="{Binding letter}" FontSize="24" FontFamily="Resources/Fonts/Programa Tutorias Bold.ttf#Programa Tutorias" Foreground="Black" VerticalAlignment="Center"/>
                <TextBlock TextWrapping="Wrap" Text="{Binding option}" FontSize="24" FontFamily="Resources/Fonts/Programa Tutorias Bold.ttf#Programa Tutorias" Width="253" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </StackPanel>
            <Grid VerticalAlignment="Center" Margin="5,10,5,0" HorizontalAlignment="Center">
                <Image Source="{Binding imageURI}" />
            </Grid>
        </StackPanel>
    </Grid>
</DataTemplate>

1 个答案:

答案 0 :(得分:27)

数据模板只能有一个可视树,但您要定义两个网格。如果您希望两个网格彼此相邻或一个在另一个之下,请将它们包裹在StackPanel中并相应地设置属性Orientation

<DataTemplate>
   <StackPanel Orientation="Vertical">
      <Grid>[...]</Grid>
      <Grid>[...]</Grid>
   </StackPanel>
</DataTemplate>