WPF: How do I create a background that repeats horizontally without scaling?

federubin picture federubin · Nov 23, 2009 · Viewed 11.3k times · Source

I would like to create a background for my window which is an image that I want repeated horizontally. So far I've tried with the ImageBrush, but this option repeats the image horizontally and vertically. Also, I don't want it to scale when user resize the window, as it makes the image look funny.

Answer

James Close picture James Close · Jun 12, 2012

If what you want to do is tile an image horizontally as you would in CSS with the simple one liner "background-repeat: repeat-x" then after some (!) trial and error what you need in XAML is this:

<ImageBrush ImageSource="Images/my-background-image.png" 
            TileMode="FlipY" 
            Stretch="Uniform"
            AlignmentY="Top"
            Viewport="0,0,90,3000"
            ViewportUnits="Absolute" />

Where the last 2 values on the Viewport attribute are the width of your image in pixels and then a very large number that is higher than your viewport height so that the image is not repeated in the Y direction within that height.