Silverlight:设置值的语法问题

时间:2010-12-10 22:23:58

标签: silverlight xaml

我正在使用Silverlight 4.以下XAML工作正常:

<UserControl.Resources>
    <ItemsPanelTemplate x:Key="WrapPanelTemplate">
        <toolkit:WrapPanel />
    </ItemsPanelTemplate>
</UserControl.Resources>

    <ItemsControl x:Name="restOfHits" 
                  ItemsSource="{Binding RestOfHits}" 
                  ItemsPanel="{StaticResource WrapPanelTemplate}"
                  ItemTemplate="{StaticResource FileTemplate}"
                  Width="500"
                  Margin="0,50,0,0"
                  />

然而,以下原因导致VS抱怨:

    <ItemsControl x:Name="restOfHits" 
                  ItemsSource="{Binding RestOfHits}" 
                  ItemTemplate="{StaticResource FileTemplate}"
                  Width="500"
                  Margin="0,50,0,0"
                  >
        <ItemsControl.ItemsPanel>
            <toolkit:WrapPanel />
        </ItemsControl.ItemsPanel>
    </ItemsControl>

错误:

  

属性'ItemsPanel'不支持   “WrapPanel”类型的值。

这是为什么?如果我不想使用资源,指定ItemsControl应该使用WrapPanel的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

因为ItemsPanel期望收到ItemsPanelTemplate而不是其他任何内容。 你是在第一个样本中做到这一点,而不是在你的第二个样本中。你的第二个应该是这样的: -

<ItemsControl x:Name="restOfHits"  
              ItemsSource="{Binding RestOfHits}"  
              ItemTemplate="{StaticResource FileTemplate}" 
              Width="500" 
              Margin="0,50,0,0" 
              > 
    <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate>  
            <toolkit:WrapPanel />  
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel> 
</ItemsControl>