Skip to content

Instantly share code, notes, and snippets.

@PiN73
Created December 23, 2018 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PiN73/f930b23d91c493d65fe74b7b9faa7aaa to your computer and use it in GitHub Desktop.
Save PiN73/f930b23d91c493d65fe74b7b9faa7aaa to your computer and use it in GitHub Desktop.
WPF TextBlock that is centered inside given rectangle in Canvas
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
class CenteredTextBlock: Grid
{
private TextBlock textBlock = new TextBlock
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
public string Text
{
get { return textBlock.Text; }
set { textBlock.Text = value; }
}
public Brush Foreground
{
get { return textBlock.Foreground; }
set { textBlock.Foreground = value; }
}
public double FontSize
{
get { return textBlock.FontSize; }
set { textBlock.FontSize = value; }
}
public CenteredTextBlock()
{
Children.Add(textBlock);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment