我似乎无法弄清楚我应该/不应该对我的约束做什么。我有一个WPF窗口,它使用嵌套对象的双向绑定。主要对象是Client,客户端对象具有住宅和邮件地址,其类型为Address对象。我可以毫无问题地保存客户端对象,但是当它试图保存地址对象时,它们是null或者我必须通过" newing"来创建它们。他们在保存之前。我假设使用数据绑定,嵌套对象将被填充,而不必做任何事情"特殊"也就是说不需要新建它们只是允许绑定做的工作?我做错了什么,或者我希望它对我做的太多了?
总之,对象看起来像这样
public class Client : People
{
public int ClientID { get; set; }
public int? ResidentialAddressID { get; set; }
[ForeignKey("ResidentialAddressID")]
public virtual Address ResidentialAddress { get; set; }
public int? MailingAddressID { get; set; }
[ForeignKey("MailingAddressID")]
public virtual Address MailingAddress { get; set; }
}
[Table("bhs.Addresses")]
public class Address
{
public int AddressID { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
public int? StateID { get; set; }
public State State { get; set; }
public string ZipCode { get; set; }
}
表格看起来像这样
People ->
PeopleID
FirstName
MiddleName
LastName
DateOfBirth
SocialSecurityNumber
Gender
Client ->
ClientID
PeopleID
ResidentialAddressID
MailingAddressID
Addresses->
AddressID
Line1
Line2
City
StateID
ZipCode
绑定看起来像这样。
<Label Content="First Name:" Grid.Column="0" Margin="0,1,0,0" Grid.Row="0" Foreground="White" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
<TextBox Grid.Column="1" Text="{Binding FirstName}" x:Name="txtFirstName" Margin="5,5,5,5" Grid.Row="0" TextWrapping="Wrap" />
<Label Content="Middle Name:" Grid.Column="0" Margin="0,1,0,0" Grid.Row="1" Foreground="White" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
<TextBox Grid.Column="1" x:Name="txtMiddleName" Margin="5,5,5,5" Grid.Row="1" TextWrapping="Wrap" Text="{Binding MiddleName}" />
<Label Content="Last Name:" Grid.Column="0" Grid.Row="2" Foreground="White" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
<TextBox Grid.Column="1" x:Name="txtLastName" Margin="5,5,5,5" Grid.Row="2" TextWrapping="Wrap" Text="{Binding LastName}" />
<Label Content="Date Of Birth:" Grid.Column="0" Grid.Row="3" Foreground="White" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
<DatePicker Grid.Column="1" x:Name="datDateOfBirth" Margin="5,5,5,5" Grid.Row="3" SelectedDate="{Binding DateOfBirth}"/>
<Label Content="Social Security Number:" Grid.Column="0" Grid.Row="4" Foreground="White" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
<xctk:MaskedTextBox x:Name="txtSocialSecurityNumber" Text="{Binding SocialSecurityNumber}" Margin="5,5,5,5" Grid.Column="1" Grid.Row="4" TextWrapping="Wrap" ClipboardMaskFormat="ExcludePromptAndLiterals" IncludeLiteralsInValue="False" Mask="000-00-0000"/>
<Label Content="Gender:" Grid.Column="0" Grid.Row="5" Foreground="White" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
<ComboBox Grid.Column="1" x:Name="cboGender" Grid.Row="5" Margin="5,5,5,5" SelectedValue="{Binding Gender}" />
<Label Content="Residential Address:" Grid.Column="2" Margin="0,1,0,0" Grid.Row="2" Foreground="White" HorizontalContentAlignment="Right" VerticalContentAlignment="Center"/>
<TextBox Grid.Column="3" Text="{Binding ResidentialAddress.Line1}" Margin="5,5,5,5" Grid.Row="0" TextWrapping="Wrap"/>
<TextBox Grid.Column="3" Text="{Binding ResidentialAddress.Line2}" Margin="5,5,5,5" Grid.Row="1" TextWrapping="Wrap" />
<TextBox Grid.Column="3" Text="{Binding ResidentialAddress.City}" Margin="5,5,5,5" Grid.Row="2" TextWrapping="Wrap" />
<ComboBox Grid.Column="3" SelectedValue="{Binding ResidentialAddress.State}" Margin="5,5,5,5" Grid.Row="3"/>
<TextBox Grid.Column="3" Text="{Binding ResidentialAddress.Zip}" Margin="5,5,5,5" Grid.Row="4" TextWrapping="Wrap" />
非常感谢任何帮助。
答案 0 :(得分:1)
您必须初始化对象。
{Binding FirstName}
可以使用,因为您要将此属性设置为某个内容。
{Binding ResidentialAddress.City}
不是因为ResidentialAddress不存在,因此您无法访问其中的字段。 WPF无法知道它需要初始化您的对象以及要使用的构造函数。
你可以做的就是获得你想要的东西:
private Address _MailingAddress;
[ForeignKey("MailingAddressID")]
public virtual Address MailingAddress
{
get
{
return _MailingAddress ?? (_MailingAddress = new Address());
}
set
{
_MailingAddress = value;
}
}
这样,您的对象在第一次访问时就会被初始化,如果它是null
。否则你可以“新起来”#34;正如你在构造函数中所说的那样。