Un TextBlock deve essere centrato in una posizione x (o y quando Orientation è verticale). ho implementato:Come centrare un TextBlock in una data posizione
TextBlock text = new TextBlock();
// Some code to define text, font, etc. here
// Turn if Orientation is vertical
if (Orientation == Orientation.Vertical)
{
text.RenderTransform = new RotateTransform() { Angle = 270 };
}
// Update, then ActualWidth is set correctly
text.UpdateLayout();
// Position of label centered to given position
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); // Add to Canvas
Questo funziona bene reale, ma è possibile farlo senza UpdateLayout
. Se rimuovo UpdateLayout
, quindi non ottengo la posizione che sto cercando, perché ActualWidth è (ovviamente) zero.
si sa che se si utilizza una griglia come contenitore si avrà subito centrato ? Accanto a questo: prova con LayoutTransform e non RenderTransform – fixagon
Qual è il controllo genitore ?? Qual è il contesto di questo codice? – loxxy
@fantasticfix Devo usare RenderTransform, perché viene eseguito in WPF e Silverlight. SL non conosce LayoutTransform. – Em1