// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using Perspex.Media;
public class TextBlock : Control
{
public static readonly PerspexProperty FontSizeProperty =
PerspexProperty.Register(
"FontSize",
inherits: true);
public static readonly PerspexProperty ForegroundProperty =
PerspexProperty.Register(
"Foreground",
defaultValue: new SolidColorBrush(0xff000000),
inherits: true);
public static readonly PerspexProperty TextProperty =
PerspexProperty.Register("Text");
public double FontSize
{
get { return this.GetValue(FontSizeProperty); }
set { this.SetValue(FontSizeProperty, value); }
}
public Brush Foreground
{
get { return this.GetValue(ForegroundProperty); }
set { this.SetValue(ForegroundProperty, value); }
}
public string Text
{
get { return this.GetValue(TextProperty); }
set { this.SetValue(TextProperty, value); }
}
private FormattedText FormattedText
{
get
{
return new FormattedText
{
FontFamilyName = "Segoe UI",
FontSize = this.FontSize,
Text = this.Text,
};
}
}
public override void Render(IDrawingContext context)
{
Brush background = this.Background;
if (background != null)
{
context.FillRectange(background, this.Bounds);
}
context.DrawText(this.Foreground, new Rect(this.Bounds.Size), this.FormattedText);
}
protected override Size MeasureContent(Size availableSize)
{
ITextService service = ServiceLocator.Get();
return service.Measure(this.FormattedText);
}
}
}