daniell
daniell

Reputation: 222

How do I make a reflection effect in WPF ? (from code)

I need to have some mirror objects in WPF. I have a Canvas with some content, and I need 50 visual clones, and if I modify something on the source, it should be updated in these clones. I know it is easy to do in XAML by binding the Visual of a VisualBrush to the element, but can's seem to do this from code.

Can anyone help ?

Upvotes: 2

Views: 4652

Answers (4)

daniell
daniell

Reputation: 222

Ok, meanwhile I have found the solution (Via Sese). If anyone is interested, find it below:

VisualBrush VisualBrush1 = new VisualBrush();
VisualBrush1.TileMode = TileMode.FlipXY;
VisualBrush1.Viewport = new Rect(0.5, 0.5, 0.5, 0.5);

Binding bb = new Binding { ElementName = "button1" };
BindingOperations.SetBinding(VisualBrush1,VisualBrush.VisualProperty, bb);
rectangle1.Fill = VisualBrush1;

and in XAML:

<Grid>
        <Button Height="39"
                Margin="82,20,87,0"
                Name="button1"
                VerticalAlignment="Top">Button</Button>
        <Rectangle Margin="82,56,87,0"
                   Name="rectangle1"
                   Height="37"
                   VerticalAlignment="Top">            
        </Rectangle>
    </Grid>

Maybe you will find this usefull, Daniel

Upvotes: 3

Tiberiu Ana
Tiberiu Ana

Reputation: 3663

If all you need is a simple reflection, here is a post linking to a tutorial and, more interestingly, a ready-made control you can just use (in Infragistics.Toybox.dll) -- make sure to first check its license though, I don't know what its status is.

http://blogs.infragistics.com/blogs/grant_hinkson/archive/2007/01/14/wpf-reflection-control.aspx

Upvotes: 1

Nir
Nir

Reputation: 29614

Here's a control I wrote a long time ago that builds the reflection effect in code:

http://www.nbdtech.com/Blog/archive/2007/11/21/WPF-Reflection-Control.aspx

Upvotes: 1

Rory
Rory

Reputation: 2742

Take a look at this example of creating an attached behavior. You could use the behavior and just create and attach an instance using code, or you could use the code in the example directly to create the reflections.

Upvotes: 1

Related Questions