有沒有辦法避免生成 ItemsControl
包裹我的項目?我的ItemsControl
綁定到VM屬性,我在ItemControl的資源中使用DataTemplate
(不包含x:Key
)來自定義集合對象的外觀。這一切都可以正常工作,但通過Snoop檢查顯示我所有的收集對象都被封裝在ContentPresenter
之內,而不是直接添加到面板中。這個事實爲我創造了一些其他問題。有沒有辦法避免額外的包裝?避免ItemsControl中的ContentPresenter
這裏的XAML:
<ItemsControl ItemsSource="{Binding Path=Children}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type vm:Ellipse}">
<Ellipse Fill="{Binding Fill}" Stroke="{Binding Stroke}" />
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Focusable="true" Margin="10" FocusVisualStyle="{x:Null}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding XLoc}" />
<Setter Property="Canvas.Top" Value="{Binding YLoc}" />
<Setter Property="Canvas.ZIndex" Value="{Binding ZOrder}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
您可能可以創建一個派生的ItemsControl並覆蓋[GetContainerForItemOverride](https://msdn.microsoft.com/en-us/library/aa346422(v = vs.110).aspx)方法來直接返回一個Ellipse控制。 – Clemens
@Clemens:它不會期望我返回一個*容器*而不是實際的要顯示的項目(另一方面是由'DataTemplate'管理的)? – dotNET
你不會再有DataTemplate了,否則你需要ContentPresenter。 – Clemens