如何动态定义这个标签?

发布于 2024-10-20 21:00:32 字数 383 浏览 0 评论 0原文

这里我有标签的 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" />

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="#FF3535A0" Content="Physics 101" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-10-27 21:00:32

就这样。希望这有帮助。工作和测试

            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);

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);
哆啦不做梦 2024-10-27 21:00:32

你需要这样的东西

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 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);
怪我入戏太深 2024-10-27 21:00:32

您可以创建一个新的 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);

或者与此相关的某些内容,属性名称和依赖项属性名称可能不准确,因为它来自内存。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文