C#WPF(GRID)如何动态创建和显示Label数组? [英] C# WPF (GRID) How to dynamically creat and show an array of Labels?
问题描述
大家好:)
[__i am ="对不起="for =""misspellings ="-=""english =""is =" t我的第一语言>< br mode ="hold"/>< br mode = "hold"/> I"m =""a =""student =""new =""to =""i ="过去24小时一直在努力解决此问题:< br mode =保持"/>< br模式=保持"/>问题描述:通过将标签拖到设计器中,添加5个代表随机diceThrow()分数的标签,以实现...< br mode ="hold" />我想通过代码创建这5个标签-创建标签数组并生成它们并在程序运行时显示它们.<<这就是我所需要的<<< br mode ="hold "/>< br mode =" hold"/>我使用了" XMAL"" XAML代码,但我正在努力解决它" s =" translation =" into =""my =""code ="没学过xmal,但只介绍了使用C#和WPF编程的知识].< br mode =" hold"/> I" using =" wpf =" application = " and =" grid.=" __]
这是我尝试用代码替换的XMAL行:
//标签Height ="28" HorizontalAlignment ="Left" Margin ="221,98,0,0" Name ="lblScoreD6" VerticalAlignment ="Top" Width ="100" Content ="0"/
//Grid.ColumnSpan ="4" Grid.Column ="1"
就我所能做到的而言:
[我的问题是**程序运行后我看不到标签**]
(也许是因为我不知道如何将此行转换为我的代码:
从xmal到c#代码的Grid.ColumnSpan ="4" Grid.Column ="1")
请指教..
提前谢谢!
艾亚尔·大卫(Eyal David),
Hi all :)
[__i am="" sorry="" for="" misspellings="" -="" english="" isn="t my 1st language><br mode="hold" /><br mode="hold" />I" m="" a="" student="" new="" to="" i="ve been trying for the last 24 hours to overcome this problem: <br mode="hold" /><br mode="hold" />Problem description: instade of adding 5 labels that represent scores of random diceThrow() by dragging the label to the Designer... <br mode="hold" />I want to create those 5 Labels - by code - creating an array of labels and generate them and present them during the program runnning time. << This is what I need<<<br mode="hold" /><br mode="hold" />I took the "XMAL" "XAML" code and I am struggling with it" s="" translation="" into="" my="" code="" didn="t learn xmal, but only introduction to programing using C# and WPF].<br mode="hold" />I" using="" wpf="" application="" and="" grid.=""__]
This is the XMAL line I''m trying to replace by code:
// Label Height="28" HorizontalAlignment="Left" Margin="221,98,0,0" Name="lblScoreD6" VerticalAlignment="Top" Width="100" Content="0" /
// Grid.ColumnSpan="4" Grid.Column="1"
This is as far as I managed to go:
[my problem is ** I CAN''T SEE THE LABELS ONCE THE PROGRAM IS RUNNING **]
(mybe it''s becouse I don''t know how to translate this line into my code:
Grid.ColumnSpan="4" Grid.Column="1" from xmal to c# code)
Please advise..
Thx on advance!
Eyal David,
public MainWindow()
{
InitializeComponent();
Label[] scoresLabelArr = new Label[5];
int location = 98;
for (int i = 0; i < scoresLabelArr.Length; i++)
{
Label currentLabel = new Label();
scoresLabelArr[i] = (Label)currentLabel;
scoresLabelArr[i].Name = "lblScoreDice" + i;
scoresLabelArr[i].Height = 28;
scoresLabelArr[i].Width = 100;
scoresLabelArr[i].HorizontalAlignment = HorizontalAlignment.Left;
scoresLabelArr[i].VerticalAlignment = VerticalAlignment.Top;
scoresLabelArr[i].Content = "0";
scoresLabelArr[i].Margin = new Thickness(211, location, 0, 0);
scoresLabelArr[i].Foreground = Brushes.Black;
//Label currentLabel = scoresLabelArr[i];
// In the designer - i've named the Grid [grid1]
// Grid grid = new Grid();
// grid.Children.Add((Label)scoresLabelArr[i]);
// grid1.CommandBindings.AddRange(Top.CompareTo) =
grid1.Children.Add(currentLabel);
location += 34;
推荐答案
它看起来就像是布局问题.尝试使用System.Windows.Controls.StackPanel
来保存标签.另外,为什么要贴标签?您最好使用System.Windows.Controls.TextBlock
.请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.controls.stackpanel.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.controls.textblock.aspx [ ^ ].
—SA
It looks like just the layout problem. Try to useSystem.Windows.Controls.StackPanel
to hold your labels. Also, why labels? You should better useSystem.Windows.Controls.TextBlock
. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.stackpanel.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.aspx[^].
—SA
Hi SA,
Thx for your knowlage.
I'm afraid I'm too new to this. The grid I'm using is the 1 that you start the application with (or i can simply drag it from the objects bar - it's called: "Grid";
However, if there are several types - I coppied the XMAL info of that grid - maybe it would help u to help me:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="156*" />
<ColumnDefinition Width="86*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="24*" />
<ColumnDefinition Width="88*" />
<ColumnDefinition Width="39*" />
</Grid.ColumnDefinitions>
Can you help me by adding the dirrect commands to my Code to display / layout those labels?
It's improtant for my project to use them as labels instead of textBlock.
Thank you very much!
Eyal :)
这篇关于C#WPF(GRID)如何动态创建和显示Label数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!