WPF 绑定不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17017467/
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 08:58:34  来源:igfitidea点击:

WPF binding Not working

c#wpfmvvmbinding

提问by Aslam Jiffry

I am new to WPF. I have developed this test MVVMApplication using WPF. But binding does not work. But as far as my knowledge no errors can be detected. can any one help on this. below images and coding shows the test app.

我是新手WPF。我已经MVVM使用WPF. 但是绑定不起作用。但据我所知,没有检测到错误。任何人都可以对此提供帮助。下面的图像和编码显示了测试应用程序。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Student.cs

Student.cs

using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using System.Text;
using System.ComponentModel;

namespace WpfNotifier
{
    public class Student : INotifyPropertyChanged
    {

    private string _name;
    public string Name
    {
        get { return this._name; }
        set
        {

            this._name = value;
            this.OnPropertyChanged("Name");
        }
    }
    private string _company;
    public string Company
    {
        get { return this._company; }
        set
        {
            this._company = value;
            this.OnPropertyChanged("Company");
        }
    }
    private string _designation;
    public string Designation
    {
        get { return this._designation; }
        set
        {
            this._designation = value;
            this.OnPropertyChanged("Designation");
        }
    }
    private Single _monthlypay;
    public Single MonthlyPay
    {
        get { return this._monthlypay; }
        set
        {
            this._monthlypay = value;
            this.OnPropertyChanged("MonthlyPay");
            this.OnPropertyChanged("AnnualPay");
        }
    }
    public Single AnnualPay
    {
        get { return 12 * this.MonthlyPay; }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
}

回答by blindmeis

binding just work for public properties, so your

绑定只适用于公共属性,所以你的

 <Grid DataContext="{Binding Path=st}"> 

did not work. you have to set this.DataContext = this; in your MainWindow ctor. btw why do you need a static student object?

不工作。你必须设置 this.DataContext = this; 在您的 MainWindow ctor中。顺便说一句,你为什么需要一个静态的学生对象?

  public MainWindow()
  {
     ...
     this.DataContext = this;
  }

with this code your datacontext is your mainwindow and now your binding can work.

使用此代码,您的数据上下文是您的主窗口,现在您的绑定可以工作了。

to check DataContext and Bindings at runtime you can use Snoop.

要在运行时检查 DataContext 和 Bindings,您可以使用Snoop

回答by Felice Pollano

Try to attach the Studentinstance scto the window DataContext. In the MainWindowcontructor add the line:

尝试将Student实例附加sc到窗口DataContext。在MainWindow构造函数中添加以下行:

this.DataContext=sc;

Many mvvm library can sort of do this automtically. Without any of this you can define an embedded resource of type Student and set it to the DataContext of the window. But as a suggestion, if you want to start with MWWM, try some already made OSS library.

许多 mvvm 库可以自动执行此操作。如果没有这些,您可以定义 Student 类型的嵌入资源并将其设置为窗口的 DataContext。但是作为一个建议,如果您想从 MWWM 开始,请尝试一些已经制作好的 OSS 库。

回答by Arushi Agrawal

In the code-behind(xaml.cs) file after the InitailizeComponent();statement add the following piece of code:

在该InitailizeComponent();语句后的代码隐藏 (xaml.cs) 文件中,添加以下代码:

this.DataContext=new Student();

Add the default constructor in Student.cs
public Student()
{
}