Using UWP BackgroundSizing

Windows 10 17763 SDK added an interesting property called BackgroundSizing to all controls with a Background. Let’s see what it can do.

BackgroundSizing is useful for controls with a transparent or semi-transparent Border as it can control whether or not the background of the control extends underneath the border. Suppose we have the following XAML:

<Border BorderThickness="40" CornerRadius="4" BorderBrush="#66000000">
<Border.Background>
<ImageBrush ImageSource="/Assets/sample.jpg" Stretch="UniformToFill" />
</Border.Background>
</Border>

By not specifying the BackgroundSizing attribute, the default value is used – InnerBorderEdge. This means the background does not extend beyond the inner edge of the border:

InnerBorderEdge BackgroundSizing example - background does not extend under the border
InnerBorderEdge BackgroundSizing

Now let’s change the BackgroundSizing to OuterBorderEdge:

<Border BackgroundSizing="OuterBorderEdge" BorderThickness="40" CornerRadius="4" BorderBrush="#66000000">
<Border.Background>
<ImageBrush ImageSource="/Assets/sample.jpg" Stretch="UniformToFill" />
</Border.Background>
</Border>

In this case, the background ImageBrush will extend below the border and thanks to the fact that BorderBrush is set to be semi-transparent, it will blend with the background and create a nice effect:

OuterBorderBrush BackgroundSizing - background extends below the border
OuterBorderBrush BackgroundSizing

The BackgroundSizing property is available when targeting Windows 10 build 17763 and newer, but as soon as WinUI 3.0 is released, it will be available for all supported versions of Windows 10!

Source code

A sample project for this article is available on my GitHub.

Buy me a coffeeBuy me a coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.