How to programmatically define a margin for a shape in WPF

advertisements

margin is of type thickness so It seems I cannot do:

Double myMargin = 10.2;
ellipse1.margin = Math.Round(myMargin  / 2,0);

So what's the syntax ?

Update: I need same margin for top left right


Margin is defined as a Thickness structure. You need to set the value to a new Thickness structure instead of a single value.

You can do:

double myMargin = 10.2;
myMargin = Math.Round(myMargin/2, 0);
ellipse1.Margin = new Thickness(myMargin);


Edit: If you want to have the thickness be different on the bottom (ie: this margin only on top, left, right), use this constructor instead:

double myMargin = 10.2;
myMargin = Math.Round(myMargin/2, 0);
ellipse1.Margin = new Thickness(myMargin, myMargin, myMargin, 0.0);