Search code examples
c#imagebrush

How to set Background image's Scretch and TileMode in Code behind?


Using C#, how to set the Background image's TileMode and Stretch property?

StackPanel1.Background = new ImageBrush(new ....); // OK
StackPanel1.Background = Stretch.Uniform; //this doesn't work...


Solution

  • Need to set the property on the image brush itself.

    var imageBrush = new ImageBrush();
    imageBrush.Stretch = Stretch.Uniform;
    StackPanel1.Background = imageBrush;