Ronald Miller
Ronald Miller

Reputation: 11

How can a method in C# be used in XAML?

I have a method creating a cone, such as in the following code snippet.

Could you explain me how this cone created by CreateCone(...) can be used in a XAML file in the same solution.

C# Code snippet :

public partial class MainWindow : Window
{
    public Window()
    {
      InitializeComponent();

      CreateCone(new Point3D(0, 0, 0), 0, 0.025, 0.1, 100, Colors.Red);         

     }
}

Upvotes: 1

Views: 120

Answers (2)

Simon P Stevens
Simon P Stevens

Reputation: 27499

If you give XAML objects a name like this:

<canvas name="myCanvas"></canvas>

Then you can access them from the code behind file using that name. Depending on the type of control it is you can usually either set the Content property, or add stuff to the controls Children collection:

myCanvas.Children.Add(mycreatedCode);

Upvotes: 1

Mark Kadlec
Mark Kadlec

Reputation: 8440

Depends when/where you want the method fired. You could call the method from a Grid event, etc. Not really sure why you would want to do that though.

Ie. <Grid Loaded="CreateConeWrapper" />, and the wrapper would call the CreateCone() function.

Upvotes: 1

Related Questions