我在使用 silverlight 双向绑定的 MVVM 模型中得到 Null 对象
我是 silverlight 的新手,并尝试使用 MVVM 模式通过 RIA 服务将表单保存到数据库。
当我在 twoway
绑定模式下将文本框绑定到字符串时,我在 ViewModel 中获得一个文本框值。
但是,当我将 Object.Property
绑定到文本框(双向绑定)时,单击“保存”按钮后,我会在 ViewModel 中得到一个 null 对象。
这是我的代码,请帮我找出哪里出错了。
private tblSchool _school;
public tblSchool thisschool
{
get
{
return _school;
}
set
{
if (_school != value)
{
_school = value;
OnPropertyChanged("thisschool");
}
}
}
private void SaveSchool()
{
DomainServiceForDatabaseData service = new DomainServiceForDatabaseData();
service.tblSchools.Add(thisschool); //HERE I GET NULL VALUE
service.SubmitChanges();
}
这是我的 XAML:
<Grid x:Name="LayoutRoot"
DataContext="{Binding Source={StaticResource SignUpViewModel}}">
<TextBox Height="23"
HorizontalAlignment="Right"
Margin="0,55,160,0"
Name="textBox1"
VerticalAlignment="Top"
Width="213"
Text="{Binding Path= thisschool.School_Name, Mode=TwoWay}" />
I'm new to silverlight and trying to save a form to the database via RIA Services using MVVM Pattern.
I get a textbox value in ViewModel when I bind a textbox to a string in twoway
binding mode.
But When I bind a Object.Property
to the textbox (Twoway binding) I get a null object in the ViewModel after I click on the save button.
Here is my code, please help me figure out where I am going wrong.
private tblSchool _school;
public tblSchool thisschool
{
get
{
return _school;
}
set
{
if (_school != value)
{
_school = value;
OnPropertyChanged("thisschool");
}
}
}
private void SaveSchool()
{
DomainServiceForDatabaseData service = new DomainServiceForDatabaseData();
service.tblSchools.Add(thisschool); //HERE I GET NULL VALUE
service.SubmitChanges();
}
Here is my XAML:
<Grid x:Name="LayoutRoot"
DataContext="{Binding Source={StaticResource SignUpViewModel}}">
<TextBox Height="23"
HorizontalAlignment="Right"
Margin="0,55,160,0"
Name="textBox1"
VerticalAlignment="Top"
Width="213"
Text="{Binding Path= thisschool.School_Name, Mode=TwoWay}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
支持字段 _school 未在代码示例中初始化。
在某个地方你需要做 _school = new tblSchool() 否则它将永远保持为空。
The backing field _school doesn't get initialized in your code sample.
Somewhere you will need to do _school = new tblSchool() or it will stay null forever.