在WPF中将属性与标签进行数据绑定时出现错误。

huangapple go评论67阅读模式
英文:

Error in Databinding a property to a label in WPF

问题

我要构建一个基本的WPF应用程序;我无法在MainView中显示学生姓名("Alexa")。以下是我的代码详细信息:

我在Model中有一个Student.cs类,如下所示

namespace Client.Model
{
    class Student:INotifyPropertyChanged
    {
        private string _name = "Alexa";

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged(nameof(Name));
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler? PropertyChanged;
    }
}

而MainViewModel.cs如下所示

class MainViewModel: ViewModelBase
{
    private Student _student;

    public Student Student
    {
        get { return _student; }
        set {
            if (_student != value)
            {
                _student = value;
            }
            OnPropertyChanged("Student");
        }
    }
}

在我的MainView中,我尝试显示学生姓名如下:

<Label FontStyle="Italic">学生姓名是:</Label>
<Label FontStyle="Italic" Content="{Binding Student.Name}"></Label>

同时,我在MainView.cs中设置了

this.DataContext = new MainViewModel();

我漏掉了什么吗?

英文:

I am to build basic application in wpf; I am not able to display student name ("Alexa") in the MainView. Here are my code details:

I have a Student.cs class in Model as shown

    namespace Client.Model
{
    class Student:INotifyPropertyChanged
    {
        private string _name =&quot;Alexa&quot;;
       

        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged(nameof(Name));
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler? PropertyChanged;
    } }

And MainViewModel.cs is

  class MainViewModel: ViewModelBase
    {
      

        public Student Student
        {
            get { return _student; }
            set {
                if (_student != value)
                {
                    _student = value;

                }
                OnPropertyChanged(&quot;_student&quot;);
                            }
        }       

    }

In my MainView, I am trying to display the student name as

 &lt;Label FontStyle=&quot;Italic&quot; &gt;Student Name is:&lt;/Label&gt;
&lt;Label FontStyle=&quot;Italic&quot; Content=&quot;{Binding Student.Name}&quot; &gt;&lt;/Label&gt;

Also I have set

this.DataContext = new MainViewModel(); in MainView.cs

What am I missing?

答案1

得分: 2

你需要在某处初始化Student属性:

class MainViewModel : ViewModelBase
{
    private Student _student = new Student(); //&lt;--

    public Student Student
    {
        get { return _student; }
        set
        {
            if (_student != value)
            {
                _student = value;
                OnPropertyChanged(nameof(Student));
            }
        }
    }
}

此外,请注意,在`MainViewModel`类中应使用`nameof`运算符来引发`PropertyChanged`事件以更新*属性*
英文:

You need to initialize the Student property somewhere:

class MainViewModel : ViewModelBase
{
    private Student _student = new Student(); //&lt;--

    public Student Student
    {
        get { return _student; }
        set
        {
            if (_student != value)
            {
                _student = value;
                OnPropertyChanged(nameof(Student));
            }
        }
    }
}

Also note that you should use the nameof operator to raise the PropertyChanged event for the property in the MainViewModel class.

huangapple
  • 本文由 发表于 2023年7月13日 20:24:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定