VisualBrush WPF displaying

1.9k Views Asked by At

I have a canvas that consists of different UserControls in WPF/XAML. In one of these controls I have a region that has to be transparent so the elements behind it are visible. However, due to the complex structure of this control I cannot just set the background to transparent or anything because there is another element in that usercontrol that is lets say moving behind it and cannot be transparent. So I want to have a region that seems transparent eventhough there is something else still behind it.

The only thing I could come up with is to use a VisualBrush and display exactly that region of my canvas on top of the region that has to look like if it was transparent. I wanted to do this in WPF and not XAML since I need to adjust the viewbox exactly so that the content is exactly the content that is behind that part. However, this does not seem to work at all. Here is the WPF code:

Rectangle rect = new Rectangle();
rect.HorizontalAlignment = HorizontalAlignment.Stretch;
rect.VerticalAlignment = VerticalAlignment.Stretch;

VisualBrush VisualBrush1 = new VisualBrush();
VisualBrush1.Visual = MainCanvas;
VisualBrush1.Stretch = Stretch.None;
rect.Fill = VisualBrush1;

RectTest.Child = rect;

BorderTest is the border that I defined in the xaml code that has to be transparent (it has a certain width and height). I did not set the viewbox properties yet since this is already not working. I was expecting that at least some part of the canvas would already be displayed inside my "borderTest"... Someone has an idea of my problem or can point me to some code where they do similar stuff? I already found a lot on the internet but I cannot get this thing to work.

1

There are 1 best solutions below

0
On

Ok, I solved this in a different way: I added a clipping region over my canvas in my usercontrol at the region where it needs to be transparent so nothing is displayed there and thus the canvas in the background becomes visible. Thanks anyway!