2017-07-01 4 views
-2

バインディングで何をすべきではないかを理解できないようです。私はネストされたオブジェクトの双方向バインディングを使用するWPFウィンドウを持っています。プライマリオブジェクトはクライアントです。クライアントオブジェクトには、アドレスオブジェクトタイプの住宅および郵送のアドレスがあります。クライアントオブジェクトを問題なく保存することができますが、アドレスオブジェクトを保存しようとするとnullになるか、または保存前にそれらを「新しく作成」する必要があります。私は、ネストされたオブジェクトは何もしなくても "特別な"別名を入力するだけで、バインディングを許可する必要はありません。私は間違ったことをしているのですか、それとも私にはあまりにも多くのことをすることを期待していますか?null参照例外エンティティフレームワークのWPFウィンドウへのバインド

要約すると、オブジェクトがこの

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" /> 

ご協力いただきありがとうございます。

答えて

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であれば、あなたのオブジェクトは、それが最初にアクセスされたときに初期化されるようにするため。それ以外の場合は、コンストラクタで言うように "新しいもの"を作成することができます。