wpf 无法将类型“字符串”隐式转换为“System.Windows.Controls.Label”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19188677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:47:22  来源:igfitidea点击:

Cannot implicitly convert type 'string' to 'System.Windows.Controls.Label

c#wpflabels

提问by Muneeb Khan

Hi am quite new to visual studios.

您好,我是视觉工作室的新手。

This is my code so far to make an example of an atm, where you the put the amount in and then click credit it adds to the balance and click debit and it takes it out. am doing this inc# wpf application.

到目前为止,这是我的代码,用于制作自动取款机的示例,您可以在其中输入金额,然后单击贷方,将其添加到余额中,然后单击借方,然后将其取出。我正在做这个 inc# wpf 应用程序。

so far i have this.

到目前为止我有这个。

public MainWindow()
        {
            InitializeComponent();
        }

    private double totalamount = 0;
    public string balance1;


    private void buttoncredit_Click(object sender, RoutedEventArgs e)
    {
        totalamount = totalamount + double.Parse(textboxamount.Text);

        balance1 = "Your Balance is: £";

        label2 = balance1 + totalamount;

and then this error comes along . "Cannot implicitly convert type 'string' to 'System.Windows.Controls.Label"

然后这个错误就出现了。“无法将类型‘字符串’隐式转换为‘System.Windows.Controls.Label’”

I have done this in forms . where i say label2.text = ..... and it works . but on the wpf i can't do this. if someone can guide me it will be much appricated.

我已经在表格中做到了这一点。我说 label2.text = ..... 并且它有效。但在 wpf 我不能这样做。如果有人可以指导我,那将非常有用。

thanks .

谢谢 。

回答by Lorentz Vedeler

For wpf the equivalent is:

对于 wpf,等效项是:

label2.Content = balance1 + totalamount;

回答by Hossein Narimani Rad

You want to set the Textproperty of the Labelnot the Labelitself. So try this:

你想设置TextLabel不是它Label本身的属性。所以试试这个:

In Windows form:

在 Windows 形式中

label2.Text = balance1 + totalamount;

In WPF:

在 WPF 中

label2.Content = balance1 + totalamount;