Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add StretchDirection to a VisualBrush?

Tags:

c#

wpf

Is it possible to add the attribute StretchDirection to a VisualBrush? My VisualBrush contains a media element, and the media element has this attribute, but not the VisualBrush. When I apply the StretchDirection attribute to this MediaElement, it is ignored. I am guessing because VisualBrush is overriding it's attributes.

  <VisualBrush x:Key="vb_zoneOneAdvertisement" TileMode="None" Stretch="Uniform" AlignmentX="Center" AlignmentY="Center" >
     <VisualBrush.Visual>
        <MediaElement />
     </VisualBrush.Visual>
  </VisualBrush> 
like image 535
nikotromus Avatar asked Mar 22 '18 22:03

nikotromus


1 Answers

VisualBrush takes ALL the content of VisualBrush.Visual and uses it as Brush. So imagine you use Stretch="None" with Image that is 10000x10000 in size, and you set this VisualBrush as Background of your Window. Like this code:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListViewBasicSample" Height="500" Width="500">

    <Grid>
        <Grid.Background>
            <VisualBrush TileMode="None" AlignmentX="Center" Stretch="Fill" AlignmentY="Center" >
                <VisualBrush.Visual>
                    <Grid Background="red">
                        <Image Source="wp2444179.jpg" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None" Margin="100" Height="10000" />
                    </Grid>
                </VisualBrush.Visual>
            </VisualBrush>
        </Grid.Background>
    </Grid>

</Window>

You would expect, only a small part of this image to be rendered, but no, because ALL the content of VisualBrush.Visual is taken as VisualBrush. Here is the result:

enter image description here

There is no place for Stretch property in this case. You could use margin, padding or TileMode to make some small adjustments here. Possibly, add a Container(like Grid) for your MediaElement and define Width and Height for this Container.

tl;dr; You asked if Stretch can be used on VisualBrush - it can be used, and it is properly taken into account, as in your example code. The problem is with VisualBrush.Visual. It makes (almost) no sense to use this property there, because all rendered VisualBrush.Visual is used as Brush. I would say, the more you stretch, the more is used as Brush :)

like image 183
Jonatan Dragon Avatar answered Oct 03 '22 19:10

Jonatan Dragon