这里我有一个标签的XAML,但我想用C#定义它。
任何人都可以帮我翻译成C#所以我可以在运行时创建这个标签吗?
它所在的画布名为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="#FF3535A0" Content="Physics 101" />
答案 0 :(得分:1)
你需要这样的东西
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);
答案 1 :(得分:1)
你去吧。希望这个Helps.Working和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);
答案 2 :(得分:0)
您可以创建一个新的Label实例,只需设置您列出的所有属性。
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);
或者那种效果,属性名称和依赖项属性名称可能不准确,因为它来自内存。