以编程方式在StackPanel中添加StackPanel

时间:2013-09-10 08:39:03

标签: c# wpf

我正在尝试实现在运行时制作的布局。

布局将分为2,3,4,......

例如,有一个带有1张图像的Big Box。

如果我拖放图像,它将被分成两部分。

A => A | B or B | A

如果我拖放另一张图片,它将是

            A |          | B
A | B =>    --| B  or  A | --
            C |          | C

简单的提示会很棒。

4 个答案:

答案 0 :(得分:1)

这个想法到目前为止很简单。让我们说清楚。首先,根堆栈面板让我们说它是A,将是Orientation="Horizontal"如下所示:

StackPanel A = new StackPanel();
A.Width = 300;
A.Height = 200;
A.Background = new SolidColorBrush(Colors.LightBlue);
A.Orientation = Orientation.Horizontal;

现在,一旦我们拥有A,我们知道它是根,每当我们添加一些东西时,它就会将它放在右边,这就像你想要的那样A | B或更多A | B | C。 现在,拖放图像检查 -

图像的坐标,用于检测它悬停在哪个父布局上,然后检查它是否为StackPanel(if(theLayout is StackPanel)

现在检查stackpanel的方向。 [PseudoCode] -

if(Horizontal)
{
     //Create a stackpanel with Vertical Orientation and place the image inside it
     //stackpanel_with_v_orientation.Children.Add(yourImage)
     //then add this stackpanel to theLayout like theLayout.Children.Add(stackpanel_with_v_orientation);
}
else
{
    //its over some sub panels,which are ofcourse with vertical 
    //orientation.just add this up.
}

这样的事情!让我知道它是否有意义。

答案 1 :(得分:0)

您可以使用DockPanel或WrapPanel。如果这还不够,您可以实现自己的Panel逻辑,指定如何安排项目。无论如何看看this link

答案 2 :(得分:0)

最后你想要一个这样的对接控件:http://avalondock.codeplex.com/

答案 3 :(得分:-1)

哇,你当然喜欢挑战!为此,您需要创建一个非常复杂的自定义Panel实现。在此Panel中,您可以访问Panel中的子项(项目),因此能够通过一些仔细的思考来实现您的要求。我可以为你提供一些关于这个主题的教程的链接,但在那之后,你几乎都是你自己的。

请查看这些链接中的文章以获取帮助:

How to create a Custom Layout Panel in WPF

Creating Custom Panels In WPF