Skip to content

Instantly share code, notes, and snippets.

@robfe
Created October 11, 2010 12:07
Show Gist options
  • Save robfe/620425 to your computer and use it in GitHub Desktop.
Save robfe/620425 to your computer and use it in GitHub Desktop.
Path subclass for drawing a line that fills its available space in silverlight
public class FillingLine : Path
{
Size lastFinalSize;
protected override Size MeasureOverride(Size availableSize)
{
return new Size(0, 0);
}
protected override Size ArrangeOverride(Size finalSize)
{
// track the last size so that we don't re-calculate when we don't need to.
// this is especially important since setting the Data triggers a new layout!
if (lastFinalSize != finalSize)
{
lastFinalSize = finalSize;
Data = CreatePathData(finalSize);
}
return finalSize;
}
static PathGeometry CreatePathData(Size size)
{
Rect rect;
if (size.Height == 1)
{
rect = new Rect(0, 0.5, size.Width, 0);
}
else if (size.Width == 1)
{
rect = new Rect(0.5, 0, 0, size.Height);
}
else
{
rect = new Rect(0.5, 0.5, size.Width - 0.5, size.Height - 0.5);
}
return new PathGeometry
{
Figures =
{
new PathFigure
{
StartPoint = new Point(rect.Left, rect.Top),
Segments =
{
new LineSegment {Point = new Point(rect.Right, rect.Bottom)}
}
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment