views:

362

answers:

4

hi guys,

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 ?

+1  A: 

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.

Rory
My application is a Library application, and in my case I don't have any XAML code. Bu I find a solution that's working for me. Thank you
daniell
+1  A: 

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

Nir
I cannot use the reflection control in my case. But I have found a way that's suitable for me. Thanks
daniell
+1  A: 

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

Tiberiu Ana
A: 

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

daniell