Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

wpf - How to bind a ModelVisual3D in a Viewport3D

I'm trying to bind a ModelVisual3D in a Viewport3D. It is a HelixViewport but I don't think that's my problem.

<DataTemplate>
    <StackPanel Orientation="Vertical">
        <h:HelixViewport3D ShowViewCube="False" MinHeight="80"
                ZoomExtentsWhenLoaded="True">
            <ModelVisual3D
                    Content="{Binding CargoVisual, Converter={StaticResource VisualModel3DConverter}}" />
            <h:SunLight />
        </h:HelixViewport3D>
        <Label Content="{Binding Tag}" Foreground="{DynamicResource Foreground}"
                HorizontalAlignment="Center" />
    </StackPanel>
</DataTemplate>

My Juliet.CargoVisual is a ModelVisual3D, but i got a binding error

Cannot convert from type 'Juliet.CargoVisual' to 'System.Windows.Media.Media3D.Model3D'. Consider setting a converter on the binding.

Here is said converter :

public class VisualModel3DConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is ModelVisual3D modelVisual3D)
        {
            // for testing purposes, returning a new BoxVisual3D
            return new BoxVisual3D
            {
                Center = new Point3D(),
                Length = 1
            };
        }
        else
        {
            return null;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

I have no error left, but the viewport shows nothing. If i add another model, without binding, it works perfectly. I must be doing something terribly wrong.

question from:https://stackoverflow.com/questions/65887114/how-to-bind-a-modelvisual3d-in-a-viewport3d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...