简体   繁体   English

如何重叠 WPF 图像

[英]How to overlap WPF Images

Is there a way overlap say three images to have them overlap.有没有办法重叠说三个图像让它们重叠。 I am have a case where I bring the top element to the top but it is showing the base layer underneath instead of the second.我有一个案例,我将顶部元素带到顶部,但它显示的是下面的基础层,而不是第二层。

Here is what I mean:这就是我的意思: 在此处输入图片说明

Should be this:应该是这样的: 在此处输入图片说明

The code for it:它的代码:

// Bottom Box
this.BottomBox.BackColor = Color.Transparent;
this.BottomBox.BackgroundImage = Resource.BottomBox;
this.BottomBox.Name = "BottomBox";
// Middle Box
this.Middle.BackColor = Color.Transparent;
this.Middle.BackgroundImage = Resource.MiddleBox;
this.Middle.Parent = BottomBox;
this.Middle.Name = "Middle";
// Top Box
this.TopBox.BackkColor = Color.Transparent;
this.TopBox.BackgroundImage = Resource.TopBox;
this.TopBox.Parent = MiddleBox;
this.TopBox.Name = "TopBox";

The easiest way to overlap controls/views (images, etc) in WPF is to put them inside a Canvas or a Grid...在 WPF 中重叠控件/视图(图像等)的最简单方法是将它们放在画布或网格中...

In a Canvas, you can alter there relative positions using the attached properties Canvas.Top and Canvas.Left .在 Canvas 中,您可以使用附加属性Canvas.TopCanvas.Left更改相对位置。

In a Grid, you can alter their relative positions using margins...在网格中,您可以使用边距更改它们的相对位置...

You can do this in the Constructor of the views immediately below the InitializeComponent method.您可以在 InitializeComponent 方法正下方的视图的构造函数中执行此操作。

Sample Code:示例代码:

// Bottom Box
        this.BottomBox.BackColor = Color.Transparent;
        this.BottomBox.BackgroundImage = Resource.BottomBox;
        this.BottomBox.Name = "BottomBox";
        // Middle Box
        this.Middle.BackColor = Color.Transparent;
        this.Middle.BackgroundImage = Resource.MiddleBox;
        this.Middle.Parent = BottomBox;
        this.Middle.Name = "Middle";
        // Top Box
        this.TopBox.BackkColor = Color.Transparent;
        this.TopBox.BackgroundImage = Resource.TopBox;
        this.TopBox.Parent = MiddleBox;
        this.TopBox.Name = "TopBox";          

        var canvas = new Canvas();
        canvas.Children.Add(this.BottomBox);
        canvas.Children.Add(this.TopBox);
        canvas.Children.Add(this.Middle);
        Canvas.SetLeft(this.BottomBox, 50);
        Canvas.SetTop(this.BottomBox, 100);
        Canvas.SetLeft(this.Middle, 50);
        Canvas.SetTop(this.Middle, 50);
        Canvas.SetLeft(this.TopBox, 50);
        Canvas.SetTop(this.TopBox, 0);

        //put canvas as the main element to display in the view

Try it out and leave a comment if you have any further issue.尝试一下,如果您有任何其他问题,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM