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,以便绑定可以工作。