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

冷暖自知 提交于 2019-12-24 00:23:47

问题


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



来源:https://stackoverflow.com/questions/2282107/how-do-i-make-a-reflection-effect-in-wpf-from-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!