【问题标题】:WPF onbutton click add a string in a string list?按钮上的WPF单击在字符串列表中添加字符串?
【发布时间】:2021-07-31 19:58:45
【问题描述】:

我有一个 xaml。 在那个 xaml 中,它们是一个按钮。 我该如何编程 如果我单击按钮,它会在项目中的类的字符串列表中添加一个字符串。

my class:
class Lager
{
    static public  List<string> Zutaten { get ; set; }
}

button:
private void button_Click(object sender, RoutedEventArgs e)
    {
        string anytext = "random text";
        Lager.Zutaten.Add(anytext);
    }

【问题讨论】:

  • 您的错误信息是什么?什么没有按预期工作?

标签: c# wpf xaml button


【解决方案1】:

WPF 的设计方式并不是您在此处尝试使用它的方式。我强烈建议您从一些 wpf 特定的 mvvm 教程开始。

但要回答你的问题:
从您提供的代码看来,您似乎忘记实例化“Zutaten”列表:

public static class Lager
{
    static public List<string> Zutaten { get ; set; } = new List<string>();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2014-07-17
    相关资源
    最近更新 更多