Created
July 9, 2012 17:48
-
-
Save robfe/3077863 to your computer and use it in GitHub Desktop.
TileCanvas in WPF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@robfe, this looks really handy. Are you okay with this being used by third parties under the terms of the Apache 2.0 license?