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>
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:
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With