views:

649

answers:

2

Hello,

I'm wondering if anyone knows if it's possible in XAML to have a ListBox whose DataTemplate defines the ListBoxItem as a 3d element. Something along the lines of:

                <ListBox x:Name="lst3D" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Viewport3D>
                            <Viewport2DVisual3D>
                                <Viewport2DVisual3D.Transform>
                                    <RotateTransform3D>
                                        <RotateTransform3D.Rotation>
                                            <AxisAngleRotation3D Angle="40" Axis="0, 1, 0" />
                                        </RotateTransform3D.Rotation>
                                    </RotateTransform3D>
                                </Viewport2DVisual3D.Transform>
                                <Viewport2DVisual3D.Geometry>
                                    <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0"
                        TextureCoordinates="0,0 0,1 1,1 1,0" 
                        TriangleIndices="0 1 2 0 2 3"/>
                                </Viewport2DVisual3D.Geometry>
                                <Viewport2DVisual3D.Material>
                                    <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="AliceBlue"/>
                                </Viewport2DVisual3D.Material>

                                <Label>Hello, 3D</Label> // we'd like to databind controls like this one

                            </Viewport2DVisual3D>
                        </Viewport3D>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBoxItem />
                <ListBoxItem />
                <ListBoxItem />
            </ListBox>

Anyone seen this sort of thing done/have any advice?

Greatly appreciated, bv

A: 

There's a couple of ways to do this.

You can change the DataTemplate to a ControlTemplate, and have it apply to all the items in the ListBox. But you cannot bind it to a specific data type.

The other option is move the DataTemplate to a resources section, and set the DataType to the type of the item you wish to display. Then you can use binding to bind to a property of the item etc.

Cameron MacFarland
I think I tried both of these options, and nothing is showing up, although the Items collection is populated. Would you mind showing me what you mean?
A: 

Replace the button with a content control such as,

  <ContentControl Content="{Binding}" />
It also looks like the Viewpord3D doesn't request layout space so I needed to wrap it in a <Grid Height="100" Width="100">...</Grid> to see anything.

chuckj
Thanks to you (and Cameron, whose advice was very helpful). I still hate the feeling of something working, and not entirely understanding why. I'd almost rather throw an exception! Thanks again.