如何在StackPanel添加列 [英] How to add Columns at StackPanel

查看:122
本文介绍了如何在StackPanel添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我可以问一个问题吗?我试图在StackPanel添加Columns,但我不知道如何使这个解决方案工作。



Hello can I ask a question? I tried to add Columns at StackPanel, but I've not idea, how to make this solution work.

//xaml
<Grid>
        <StackPanel x:Name="sp">
            <Grid x:Name="GridPanel" Grid.Row="0" Grid.Column="0">
                
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                
            </Grid>
        </StackPanel>
    </Grid>




//C#
TextBox tb = new TextBox();
            tb.Height = 60;
            tb.Width = 100;
            Grid.SetRow(tb, 0);
            Grid.SetColumn(tb, 0);
            //this.GridPanel.Children.Add(tb);
            Button b = new Button();
            b.Content = "Button";
            b.Width = 100;
            b.Height = 60;
            b.Click += new RoutedEventHandler(new_but);
            Grid.SetRow(b,0);
            Grid.SetColumn(b,1);
            //this.GridPanel.Children.Add(b);





如何添加列,我用 this.sp.Children.Add

推荐答案

StackPanel不支持列,它只有一列可以水平或垂直堆叠。 Grid(您确实使用它)支持列和行。您的代码甚至没有引用Grid来添加列。



您的代码几乎就在那里。下面看一下,



StackPanel doesn't support columns, it has only one column that can be stacked horizontally or vertically. Grid (which you are indeed using) supports columns and rows. Your code doesn't even refer to the Grid for adding the columns.

Your code, is almost there. Here have a look below,

sp.Children.Add(UIElement); // Adds a new element "in the stack"





你需要使用这段代码,





Whereas you need to use this code,

foreach (var child in sp.Children) {
   // check the type of children
   if(child is Grid) {
      // the grid, which supports columns.
      // Now, add columns to the "child".
   }
}





阅读这篇文章,了解如何添加以编程方式将列和行添加到网格。 https://social.msdn.microsoft.com/Forums/vstudio/en-US/18a54950-436d-4195-84a0-e2c7835c19dd/以编程方式添加列和行到网格,并在每个单元格中添加按钮控制?forum = wpf [ ^ ]



现在,当你改变定义时 child ,它会改变StackPanel中存在的网格。我可以问,为什么当一切都必须在网格中时,你甚至使用StackPanel。 Grid已作为此StackPanel的父级存在。



Read this post to learn how to add columns and rows to Grid programmatically. https://social.msdn.microsoft.com/Forums/vstudio/en-US/18a54950-436d-4195-84a0-e2c7835c19dd/programmatically-add-column-and-rows-to-grid-and-add-the-button-control-in-each-cell?forum=wpf[^]

Now, when you will alter the definition of child, it would alter the grid that is present inside the StackPanel. Can I ask, why are you even using a StackPanel when everything has to be in a Grid. Grid is already present as a parent for this StackPanel.


我只是在程序中编写了一个动态构建的600代码。你说为什么stakpanel而不是网格。因为我不知道一切都会变成这样!只需在文本框附近添加一个Knop来更改数据库
I just wrote a 600 code in the program all built dynamically. And you say why stakpanel instead grid. Because I did not know that everything will turn so! just had to add one Knop near textbox to change the database


这篇关于如何在StackPanel添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆