-->

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

2019-07-20 09:55发布

问题:

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:

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



回答2:

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.



回答3:

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



回答4:

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