Home > OS >  Clear WPF-Helix toolkit 3D View using buttons
Clear WPF-Helix toolkit 3D View using buttons

Time:10-12

I am using WPF with the Helix Toolkit to animate 3D meshes. I have the data as Points and I create the final mesh with these data. I took from Helix examples the SimpleDemo project and in my MainViewModel.cs file I have this code:

public class MainViewModel : INotifyPropertyChanged
    {
        MeshBuilder meshBuilder;
        MeshGeometry3D mesh;
        Model3DGroup modelGroup;

        public MainViewModel()
        {
            var greenMaterial = MaterialHelper.CreateMaterial(Colors.Green);

            modelGroup = new Model3DGroup();

            // Create a mesh builder and add a box to it
            meshBuilder = new MeshBuilder(false, false);
            meshBuilder.AddBox(new Point3D(1, 1, 1), 1, 1, 1);

            // Create a mesh from the builder (and freeze it)
            mesh = meshBuilder.ToMesh(true);


            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Material = greenMaterial, BackMaterial = greenMaterial });

            Model = modelGroup;
            ButtonClearAsync();
        }
...
rest code
...

here I create a single cube and then I add it on the Model. ButtonClearAsync Method is the one that clears the Model.

public async Task RemoveModel()
        {

            modelGroup = new Model3DGroup();
            await Task.Delay(2000);
            Model = null;

            OnPropertyChanged(nameof(Model));

            meshBuilder = new MeshBuilder(false, false);
            meshBuilder.AddBox(new Point3D(5, 5, 5), 2, 2, 2);

            mesh = meshBuilder.ToMesh(true);

            var greenMaterial = MaterialHelper.CreateMaterial(Colors.Green);

            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Material = greenMaterial, BackMaterial = greenMaterial });

            Model = modelGroup;
            OnPropertyChanged(nameof(Model));
        }

It waits 2 seconds, clears the models and re-draws the current model with other object. The event handler works great.

the issue is that I need to clear and redraw the Model whenever a button is pressed. But in MainWindow.xaml and MainWindow.xaml.cs files in order to call the function inside of MainViewModel I must create a class object. But if I do this, I think another Model instance will be created due to the constructor of MainViewModel and the Model is never clear or changed at all.

It never clears because the event handler returns always null and nothing is done. It works only if I run the Clear method inside the MainViewModel constructor, but this is not the thing I want.

Any suggestions on what should I do? Thanks in advance

CodePudding user response:

If you need to call a method on the viewmodel, try:

if(this.DataContext is MainViewModel mvm){
    mvm.MyFunctionToCall();
}

this uses the current datacontext of the MainWindow, so you should not need to create any new MainViewModel object.

  • Related