Skip to content

Instantly share code, notes, and snippets.

@robfe
Created July 9, 2012 17:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save robfe/3077863 to your computer and use it in GitHub Desktop.
TileCanvas in WPF
public class TileCanvas : Canvas
{
public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(TileCanvas), new UIPropertyMetadata(null, OnImageSourceChanged));
private static void OnImageSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
((TileCanvas)o).Rebuild();
}
public ImageSource ImageSource
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
Rebuild();
}
private void Rebuild()
{
if(ImageSource == null)
{
return;
}
var size = RenderSize;
Children.Clear();
var width = (int)ImageSource.Width;
var height = (int)ImageSource.Height;
for (int x = 0; x < size.Width; x += width)
{
for (int y = 0; y < size.Height; y += height)
{
var image = new Image { Source = ImageSource };
Canvas.SetLeft(image, x);
Canvas.SetTop(image, y);
Children.Add(image);
}
}
Clip = new RectangleGeometry(new Rect(size));
}
}
@robfe
Copy link
Author

robfe commented Jul 9, 2012

how simple https://gist.github.com/3006848 would have been in WPF

@smuckwell
Copy link

@robfe, this looks really handy. Are you okay with this being used by third parties under the terms of the Apache 2.0 license?

@smuckwell
Copy link

@robfe, thank you for getting back to me: https://twitter.com/robfe/status/433771487833497601

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment