开发者

How can I dynamically define this label?

Here I have the XAML for a label, but I want to define it in C#.

Can anyone help me translate this over to C# so I can create this label at run time?

The canvas it is in is called left_canvas.

<Label Canvas.Left="10" Canvas.Top="10" Canvas.Right="10" Height="30" Width="280"  
       Name="classname_label" FontFamily="MS Reference Sans Serif" FontSize="16"
       FontWeight="Bold" Foreground="#F开发者_StackOverflow社区F3535A0" Content="Physics 101" />


There you go.Hope this Helps.Working and Tested

            BrushConverter bc = new BrushConverter();
            Label classname_label = new Label();
            classname_label.Content = "Physics 101";
            classname_label.Foreground = (Brush)bc.ConvertFrom("#FF3535A0");
            Canvas.SetLeft(classname_label, 10);
            Canvas.SetTop(classname_label, 10);
            Canvas.SetRight(classname_label, 10);
            classname_label.Height=30;
            classname_label.Width=280;
            classname_label.FontFamily =new System.Windows.Media.FontFamily("MS Reference Sans Serif");
            classname_label.FontSize=16;
            classname_label.FontWeight = System.Windows.FontWeights.Bold;
            //Control you want to contain label 
            left_canvas.Controls.Add(classname_label);


You need something like this

Label label1 = new Label();
label1.Content = "Physics 101";
label1.Width = 280;
label1.Height = 30;
label1.SetValue(Canvas.LeftProperty, 10.0);
left_canvas.Children.Add(label1);


You can create a new Label instance and simply set all of the attributes you've listed.

Label label = new Label();

label.Height = new Unit(30, UnitType.Pixel);
...
label.Content = "Physics 101";

//Set the dependency properties.
label.SetValue(Canvas.Left, 10);
...
label.SetValue(Canvas.Top, 10);

//Add it to the canvas.
left_canvas.Controls.Add(label);

Or something to that effect, property names, and dependency property names may be inaccurate as it's from memory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜