I get Null Object in MVVM Model with silverlight two way binding
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.
B开发者_JS百科ut 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}" />
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.
精彩评论