Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I assume that you wrote this code in the constructor. It would be a reason why ActualWidth has no value. <strong>All the code hasn't been tested</strong> (no IDE available). You need to do this after the Loaded Event, after WPF has built the layout. This is a routed event, btw.</p> <pre><code>public class Class1{ public Class1 () { this.Loaded += (sender, args) =&gt; { TextBlock text = new TextBlock(); if (Orientation == Orientation.Vertical) { text.RenderTransform = new RotateTransform() { Angle = 270 }; } double halfWidth = text.ActualWidth / 2; double x1 = (Orientation == Orientation.Horizontal) ? x - halfWidth : x; double y1 = (Orientation == Orientation.Horizontal) ? y : y + halfWidth; Canvas.SetLeft(text, x1); Canvas.SetTop(text, y1); Children.Add(text); }; } </code></pre> <p>This code will probably work. Of course, the way I read your code, this code seems to be situated in the constructor from a class that you derived from Canvas. There should normally be no need to do so, except when you really need to extend the core functionality of the canvas control. You can make reusable components on top of existing controls by creating a UserControl. You should especially use this approach if you don't need to override any method of the Canvas. </p> <p>Alternatively, if you just want to have a centered item inside a container, the following xaml will do that just fine:</p> <pre><code> &lt;Grid&gt; &lt;TextBlock Text="Haha!" VerticalAlignment="Center" HorizontalAlignment="Center"/&gt; &lt;/Grid&gt; </code></pre>
 

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