C#WPF需要有关代码的帮助... [英] C# WPF Need help with code...

查看:49
本文介绍了C#WPF需要有关代码的帮助...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

This is my code

   private void button1_Click(object sender, RoutedEventArgs e)
       {
           DateTime dt1 = Convert.ToDateTime(label1.Content + "/" + label2.Content + "/" + label5.Content);
           DateTime dt2 = Convert.ToDateTime(label3.Content + "/" + label4.Content + "/" + label6.Content);
           TimeSpan ts =  dt2-dt1 ;
           int days = ts.Days;
           label14.Content = ts.Days;
           for (int i = 0; i < 50; i++)
           {
               TextBlock TXB = new TextBlock();
               TXB.Background = new LinearGradientBrush(Colors.Red, Colors.LightCyan, new Point(0, 0), new Point(1, 1));
               TXB.Text =  i.ToString();
               TXB.HorizontalAlignment = HorizontalAlignment.Left; TXB.VerticalAlignment = VerticalAlignment.Stretch;
               TXB.Width = 30;
               TXB.Name = "T" + i.ToString();
               if (TXB.Margin == TXB.Margin)
               {
                   TXB.Margin = new Thickness(TXB.Margin.Left,  0, TXB.Margin.Right + 20, 0);

               }
this.grid2.Children.Add(TXB);

           }



现在这就是我想要做的,我试图做到这一点,因此当我单击此按钮时,grid1中将出现一行textbolocks,例如

我= textblock
文本块号= 6
我location.right = +1

因此,它像
那样排成一行 iiiiii等...
并且连续有6个i



Now this is what i am trying to do, im trying to make it so when i click this button a row of textbolocks will come up in grid1, for example

i = textblock
textblock number = 6
i location.right = +1

so then its in like a row like
iiiiii etc etc...
and there are 6 i''s in a row

推荐答案

您可以做您正在做的事情,但是要插入TextBlocks的Grid需要具有ColumnDefinitions每个TextBlock,否则它们都将位于同一Grid单元中.

您可以为网格定义6列,然后为每个TextBlock设置Grid.Column附加属性,以使其进入正确的列.

为什么不使用Orientation = Horizo​​ntal而不是Grid的StackPanel?然后,系统会在您插入它们时为您布置它们.
You can do what you are doing, but the Grid you are inserting the TextBlocks into needs to have ColumnDefinitions for each TextBlock otherwise they will all be in the same Grid cell.

You can define the 6 columns for the grid and then set the Grid.Column attached properrty for each TextBlock so it goes in the correct column.

Why not use a StackPanel with Orientation=Horizontal instead of a Grid? Then the system will lay them out for you as you insert them.


我认为您只想设置要创建的控件的left和top属性,然后将它们放置在你要.是吗?
I think you just want to set the left and top properties of the controls you''re creating, to put them where you want. Is that right ?


这篇关于C#WPF需要有关代码的帮助...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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