tags:

views:

1438

answers:

2

Hi,

I am learning some WPF/C# so this question might be silly for some of you but I can't find the solution.

I have a Canvas (let's say it countains texts) and then I have another Canvas that I want to use as a mask (anything in this second Canvas will mask the first one).

How can I do that? What if the second Canvas is moving? I want to change the scale of the second canvas (mask) so it "unveils" the content of the first one (content).

Thanks in advance!

EDIT: If instead of a canvas I could use something like a rectangle (as soon as it's resizable) that's fine too!

EDIT 2: Here's the code I'm using:

// Order info
Canvas order_info = new Canvas();
order_info.Width = 220;
order_info.Height = 250;
order_info.Background = Brushes.Yellow;
user_info.Children.Add(order_info);
// Order info mask
Canvas order_info_mask = new Canvas();
order_info_mask.Width = 110;
order_info_mask.Height = 250;
order_info_mask.Background = Brushes.Pink;
user_info.Children.Add(order_info_mask);
// Apply mask
VisualBrush mask_brush = new VisualBrush();
mask_brush.Visual = order_info_mask;
order_info.OpacityMask = mask_brush;
A: 

You probabbly want to use the OpacityMask property on your Canvas instead, which is of type Brush, which could be animated.

Edit in response to question: You can do something like:

VisualBrush b = new VisualBrush();
b.Visual = canvas2;
canvas2.OpacityMask = b;

You can also set this in xaml with databinding:

<Canvas>
    <Canvas.OpacityMask>
        <VisualBrush Visual="{Binding ElementName=canvas2}" />
    </Canvas.OpacityMask>
</Canvas>

...

<Canvas x:Name="canvas2" ... />

However, to me, this seems like the incorrect approach, what visual transition are you are exactly?

Phil Price
A: 

Mind, I am no WPF expert, but you should start with embedding two canvasses in a 1x1 grid:

<Grid>
   <Canvas x:Name="background">
   </Canvas>
   <Canvas x:Name="foreground">
   </Canvas>
</Grid>

In this way the content of the canvases will be drawn on top of each other. Inside the foreground Canvas you can draw your rectangles etc. Hope this helps to give you a start!

Dabblernl
Thanks for the suggestion but it's not exactly what I'm looking for.I don't want to "cover" one div with another but show only a part of it (while you can still see the background and everything. So a Mask is what I need.Thanks though!
ozke