Search code examples
wpfviewport3d

Is there something like ItemsControl for use inside a Viewport3D?


I have a Viewport3D with a few items in it, and I want to add additional items to it that are from a collection that can be data bound to. Is there something like that, that would allow code like this:

<Viewport3D>
     <Viewport3D.Camera...>
     <ModelVisual3D>
         <ModelVisual3D.Content>
             <AmbientLight Color="White"/>
         </ModelVisual3D.Content>
     </ModelVisual3D>

     <ItemsControl ItemsSource="{Binding MyCollection}">
         <ItemsControl.ItemTemplate>
              <DataTemplate ...>
                   <ModelVisual3D ....>
              </DataTemplate>
         </ItemsControl.ItemTemplate>
     </ItemsControl>
</Viewport3D>

Solution

  • My solution to this was to use a standard itemscontrol, and stack Viewport3D controls on top of each other. This doesn't respect depth-ordering, but in my case I wanted the items in the itemscontrol to be in front of the rest of the elements anyways:

        <ItemsControl ItemsSource="{Binding XXX}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type XXX}">
                    <Viewport3D Camera="{Binding Camera, ElementName=MainViewport}">
                        <ModelVisual3D>
                            <ModelVisual3D.Content>
                                <AmbientLight Color="White"/>
                            </ModelVisual3D.Content>
                        </ModelVisual3D>
    
                        <ModelVisual3D>
                          ...my template here...
                        </ModelVisual3D>
                    </Viewport3D>
    
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>