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