Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't do this because although <code>Margin.Left</code> gets or sets, it's not used like a variable. Just like <code>Padding</code>.</p> <p>As you may notice, <code>Margin</code> (as for <code>Padding</code>) is a property and <code>Thickness</code> is its value. So, when you say</p> <pre><code>label1.Margin.Left = MainGrid.ActualHeight / 2 - label1.ActualHeight / 2; </code></pre> <p>you are only editing a copy <em>(<code>Margin</code>)</em> because <code>Margin</code> returns a struct (<code>Thickness</code>) . That's why you receive an error. The changes are not saved because it's a copy!</p> <p>Basically, although you can change the value of <code>Object.Margin.Left</code> to a specific value yet it wouldn't save/change the object <code>Margin</code> property and that's why you get an error.</p> <hr> <h2>For example</h2> <p>We can NOT say </p> <pre><code>label1.Margin.Left = MainGrid.ActualHeight / 2 - label1.ActualHeight /2; </code></pre> <p>but we can say</p> <pre><code>Thickness NewThickness = new Thickness(); //Initialize a new Thicnkess NewThickness.Left = MainGrid.ActualHeight / 2 - label1.ActualHeight /2; //Set the left property of NewThickness label1.Margin = NewThickness; //Apply the changes to label1 </code></pre> <p>This means that, because <code>Margin</code> is a property, you are not allowed to change its <code>Thickness</code> directly.</p> <hr> <p>Alternatively, you can use the following which I believe to be easier to set a <code>Margin</code> of an object</p> <pre><code>label1.Margin = new Thickness(double left, double top, double right, double bottom); </code></pre> <p>Thanks, <br>I hope you find this helpful :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload