Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF绑定不工作_C#_Wpf_Mvvm_Binding - Fatal编程技术网

C# WPF绑定不工作

C# WPF绑定不工作,c#,wpf,mvvm,binding,C#,Wpf,Mvvm,Binding,我是新来的WPF。我使用WPF开发了这个测试MVVM应用程序。但绑定不起作用。但据我所知,没有错误可以被发现。谁能帮上忙吗。下面的图片和编码显示了测试应用程序 Student.cs using System;<br/> using System.Collections.Generic;<br/> using System.Linq;<br/> using System.Text; using System.ComponentModel; namespac

我是新来的
WPF
。我使用
WPF
开发了这个测试
MVVM
应用程序。但绑定不起作用。但据我所知,没有错误可以被发现。谁能帮上忙吗。下面的图片和编码显示了测试应用程序

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));
        }
    }
}
}
使用系统
使用System.Collections.Generic
使用System.Linq
使用系统文本; 使用系统组件模型; 命名空间WpfNotifier { 公共班级学生:InotifyProperty已更改 { 私有字符串\u名称; 公共字符串名 { 获取{返回此。_name;} 设置 { 这个._name=value; 本协议项下的不动产变更(“名称”); } } 私人字符串公司; 公共字符串公司 { 获取{返回此。_company;} 设置 { 本公司=价值; 本不动产变更(“公司”); } } 专用字符串_指定; 公共字符串名称 { 获取{返回此。_;} 设置 { 这是。_名称=值; 本条关于财产变更(“指定”); } } 私人单人月付; 公共单月付款 { 获取{返回此。\每月支付;} 设置 { 这个。_monthlypay=值; 此项。不动产变更(“月付款”); 本公司的不动产发生变更(“年费”); } } 公共单一年费 { 获取{return 12*this.MonthlyPay;} } 公共事件属性更改事件处理程序属性更改; 私有void OnPropertyChanged(字符串propertyName) { var handler=PropertyChanged; if(处理程序!=null) { 处理程序(这是新的PropertyChangedEventArgs(propertyName)); } } } }
尝试将
学生
实例
sc
附加到窗口
DataContext
。 在
main窗口中
constructor添加行:

this.DataContext=sc;
许多mvvm库可以自动完成这项工作。如果没有这些,您可以定义Student类型的嵌入式资源,并将其设置为窗口的DataContext。
但是作为一个建议,如果你想从MWM开始,尝试一些已经制作好的OSS库。

尝试将
学生
实例
sc
附加到窗口
DataContext
。 在
main窗口中
constructor添加行:

this.DataContext=sc;
许多mvvm库可以自动完成这项工作。如果没有这些,您可以定义Student类型的嵌入式资源,并将其设置为窗口的DataContext。 但是作为建议,如果您想从MWM开始,请尝试一些已经制作好的OSS库。

InitailizeComponent()之后的代码隐藏(xaml.cs)文件中语句添加以下代码段:

this.DataContext=new Student();

Add the default constructor in Student.cs
public Student()
{
}
InitailizeComponent()之后的代码隐藏(xaml.cs)文件中语句添加以下代码段:

this.DataContext=new Student();

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

绑定只适用于公共属性,因此

 <Grid DataContext="{Binding Path=st}"> 
有了这段代码,datacontext就是主窗口,现在绑定就可以工作了


要在运行时检查DataContext和绑定,可以使用。

绑定只适用于公共属性,因此

 <Grid DataContext="{Binding Path=st}"> 
有了这段代码,datacontext就是主窗口,现在绑定就可以工作了



要在运行时检查DataContext和绑定,您可以使用。

而不是附加映像,您可以附加xaml、cs代码本身。谢谢。。Dj将在下次调试时在输出窗口中显示绑定中的错误。如果你有一些,分享会有帮助!您可以附加xaml、cs代码本身,而不是附加图像..谢谢。。Dj将在下次调试时在输出窗口中显示绑定中的错误。如果你有一些,分享会有帮助!谢谢请回答。Arushi我只是想在XAML中使用Datacontext,而不是代码隐藏。有可能吗。。请回答。Arushi我只是想在XAML中使用Datacontext,而不是代码隐藏。有没有可能工作得很好。。但是有没有办法不在codbehind中使用DataContext。。。我的意思是只在XAML中使用DataContext,这很好。。但是有没有办法不在codbehind中使用DataContext。。。我的意思是只在XAML中使用DataContext,然后你应该更新你的问题,因为st只是一个静态的field@Arkenstone:您的屏幕截图清楚地显示了
公共静态学生st=new Student()
-这是一个字段,不是属性。@Arkenstone:Properties:Fields:
公共学生st{get;set;}
我将字段更改为属性。但仍然没有结果显示在装订文本框见我的编辑请。您需要正确的datacontext,以便绑定可以工作。然后您应该更新您的问题,因为这里只有一个静态field@Arkenstone:您的屏幕截图清楚地显示了
公共静态学生st=new Student()
-这是一个字段,不是属性。@Arkenstone:Properties:Fields:
公共学生st{get;set;}
我将字段更改为属性。但仍然没有结果显示在装订文本框见我的编辑请。您需要正确的datacontext,以便绑定可以工作。