简体   繁体   English

在WPF中将控件在运行时移动到另一个Groupbox

[英]Moving controls to another groupbox during runtime in wpf

I have two groupboxes. 我有两个分组框。 GroupBox1 and GroupBox2. GroupBox1和GroupBox2。 Is it possible to move controls inside GroupBox1 to GroupBox2 during runtime ? 是否可以移动控件内部GroupBox1GroupBox2运行时?

For example in the following xaml file if I want to move the label1 in GroupBox1 to GroupBox2 during run time. 例如在下面的xaml文件,如果我想在移动label1GroupBox1GroupBox2在运行时。 How should I do it ? 我该怎么办?

<Grid>
    <GroupBox Name="GroupBox1" HorizontalAlignment="Left" Height="400" VerticalAlignment="Top" Width="330" >
                <StackPanel Orientation="Horizontal">
                        <Label Content="MyContent" x:Name="label1"  FontSize="16"/>
                        <Image Name="ImageName1"  VerticalAlignment="Bottom" Width="62" Height="42"/>
                </StackPanel>
    </Groupbox>
    <GroupBox Name="GroupBox2" HorizontalAlignment="Left" Height="400" VerticalAlignment="Top" Width="330" >
                <StackPanel Orientation="Horizontal">
                        <Label Content="MyContent" x:Name="label2" />
                        <Image Name="ImageName2"  VerticalAlignment="Bottom" Width="62" Height="42"/>
                </StackPanel>
    </Groupbox>

</Grid>

Note : I do know that I can transform the position of controls during run time by changing their margin value like this: 注意:我知道我可以通过在控件运行时更改其边距值来改变控件的位置,如下所示:

test.Margin = new Thickness(0, -5, 0, 0);

Just remove it from one StackPanel and put into another. 只需将其从一个StackPanel删除,然后放入另一个中即可。

stackpanel1.Children.RemoveAt(stackpanel1.Children.IndexOf(label1));
stackpanel2.Children.Add(label1);

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

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