我在Silverlight中使用Prism 4,我想使用ItemsControl来托管多个视图。我真的希望将所有视图都包装在指定的ItemTemplate中,或者能够指定ItemStyle,以便我可以使用类似Silverlight Toolkit中的Expander控件的东西。当我尝试指定ItemTemplate时,会在运行时引发未处理的System.NotSupportedException。是否有类似于TabControlRegionAdapter.ItemContainerStyle ItemsControl的附加属性?
ItemsControl.Items must not be a UIElement type when an ItemTemplate is set.
at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.GetContainerForItem(Object item, DependencyObject recycledContainer)
at System.Windows.Controls.ItemContainerGenerator.Generator.GenerateNext(Boolean stopAtRealized, Boolean& isNewlyRealized)
at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.GenerateNext(Boolean& isNewlyRealized)
at System.Windows.Controls.ItemsControl.AddContainers()
at System.Windows.Controls.ItemsControl.RecreateVisualChildren(IntPtr unmanagedObj)
代码
<ItemsControl Regions:RegionManager.RegionName="DetailsRegion">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<ContentPresenter Content="{Binding}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
你是如何设置的ItemsSource?基于异常,它听起来像你显式地将项目添加到项目控件(itemsControl.itemsControl.Items.Add()),而不是设置ItemsSource,通常通过绑定或您可以通过Codebehind(itemscontrol.ItemsSource =我的收藏)。 – 2012-01-22 09:32:54
我没有在Xaml中设置ItemsSource。我在Prism4中使用默认的ItemsControlRegionAdapter。它将void Adapt(IRegion region,ItemsControl regionTarget)中的ItemsSource设置为:regionTarget.ItemsSource = region.Views。 region.Views是IViewsCollection:IEnumerable
你知道收集什么吗?我认为这可能是问题,因为SL知道它已经是一个可能只是尝试渲染它的元素,而不是将它用作DataTemplate的DataContext。 – 2012-01-26 12:28:43