XAML 错误:“多次设置属性 'VisualTree'" [英] XAML error: "The property 'VisualTree' is set more than once"

查看:40
本文介绍了XAML 错误:“多次设置属性 'VisualTree'"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to put two Grids in a DataTemplate.

我的代码出现以下错误.

I'm getting the following error with my code shown below.

错误:属性 'VisualTree' 设置不止一次"

Error: "The property 'VisualTree' is set more than once"

<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>

推荐答案

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

A data template can only have one visual tree but you are defining two grids. If you want the two grids to appear next to each other or one below the other, wrap them in a StackPanel and set the property Orientation accordingly.

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

这篇关于XAML 错误:“多次设置属性 'VisualTree'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆