2

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.

2
  • This will only work if the UserControl is not itself a child of the Canvas.
    – Clemens
    Aug 4, 2014 at 19:32
  • Ok that might be the problem because it is a child of the Canvas. What is the reason behind this (that it cannot be a child of the canvas)? Is there another way to do this?
    – alcon
    Aug 5, 2014 at 8:52

1 Answer 1

1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.