我想在运行时从WPF中(xaml.cs)后面的代码创建一个新标签。因为我想创建许多对象,所以无法在xaml文件的开头创建该对象。

我在xaml.cs文件中尝试过的操作:

Label newLabel = new Label();
newLabel.Text = "newLabelText";
myCanvas.Children.Add(newLabel);    // Error at newLabel


我的xaml文件的一部分:

<Canvas x:Name="myCanvas" ...


我收到以下错误。

Cannot convert fromt 'System.Windows.Forms.Label' to 'System.Windows.UIElement'


我在网络上找不到解决方案。

最佳答案

您正在混合两个不同的名称空间。您的标签属于System.Windows.Forms命名空间。您需要创建System.Windows.Control标签。

希望这段代码能起作用,

System.Windows.Controls.Label newLabel = new System.Windows.Controls.Label();
newLabel.Context = "newLabelText";
myCanvas.Children.Add(newLabel);

关于c# - 无法从System.Windows.Forms.Label转换为System.Windows.UIElements,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33561499/

10-14 19:44
查看更多