Client と呼ばれるビュー クラスがあり、そのビュー モデルは ClientViewModel です。ClientViewModel にはモデル オブジェクト ClientInfo があります。この ClientInfo [モデル] は、Client および ClientProfile と呼ばれるモデル クラスのプロパティを持つ複合オブジェクトです。
次のように、UI 要素のプロパティを View にバインドしました (xxx.yyy.zzz を使用してプロパティにアクセスします)。
<Label Content="First Name:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3,5,0,4" VerticalAlignment="Center" Height="26" Width="70" />
<TextBox Grid.Column="1" Grid.Row="1" Height="24" HorizontalAlignment="Left" Margin="3,7,0,4" Name="firstNameTextBox" Text="{Binding Path=ClientInfo.Client.FirstName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
<Label Content="Last Name:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="3,3,0,6" VerticalAlignment="Center" Height="26" Width="69" />
<TextBox Grid.Column="1" Grid.Row="2" Height="24" HorizontalAlignment="Left" Margin="3,5,0,6" Name="lastNameTextBox" Text="{Binding Path=ClientInfo.Client.LastName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" />
..
<Button Content="Save" Height="24" Grid.Column="0" Grid.Row="0" Command="{Binding SubmitCommand}" Cursor="Hand" Margin="549,10,10,0" Name="button1" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.137,-1.804" />
クライアントビューモデル:
[Export(typeof(ClientViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ClientViewModel : NotificationObject
{
private readonly IClientService clientService;
private ClientInfo clientInfoModel;
private string currentState;
public DelegateCommand<object> SubmitCommand { get; private set; }
public DelegateCommand<object> UpdateCommand { get; private set; }
public DelegateCommand<object> LoadCommand { get; private set; }
[Import]
public ClientInfo ClientInfoModel
{
get { return this.clientInfoModel; }
set
{
clientInfoModel = value;
this.RaisePropertyChanged(() => this.ClientInfoModel);
}
}
[ImportingConstructor]
public ClientViewModel(IClientService clientService)
{
this.clientService = clientService;
this.SubmitCommand = new DelegateCommand<object>(this.Submit);
this.UpdateCommand = new DelegateCommand<object>(this.Update);
this.LoadCommand = new DelegateCommand<object>(this.Load);
}
private void Load(object obj)
{
throw new NotImplementedException();
}
private void Update(object obj)
{
//update
throw new NotImplementedException();
}
private void Submit(object obj)
{
string s = this.ClientInfoModel.ClientBasic.FirstName;//<--- this where i get the NPE exception
}
public string ViewName
{
get { return "Client Details"; }
}
public string CurrentState
{
get
{
return this.currentState;
}
set
{
if (this.currentState == value)
{
return;
}
this.currentState = value;
this.RaisePropertyChanged(() => this.CurrentState);
}
}
public bool CanSubmit
{
get { return true; }
}
public void Submit()
{
this.CurrentState = "Submitting";
//this.clientRepository.SaveClientAsync(this.ClientInfoModel, result => { SaveClient(); });
}
private object SaveClient()
{
this.CurrentState = "Saving";
return null;
}
}
ClientInfo (モデル):
public class ClientInfo : DomainObject
{
public Client ClientBasic { get; set; }
public ClientProfile Profile { get; set; }
}
クライアント (モデル):
public class Client : DomainObject
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
....
}
submit コマンドの呼び出しで:
private void Submit(object obj)
{
ClientInfo ci = new ClientInfo();
ci.Client <-- (here i would want to get the new Client obj assigned from properties?)
ci.ClientProfile <---(same as above)
}
ビューには、保存コマンドで保存するための送信ボタンがあります。いくつかのサービスを呼び出す新しいクライアント オブジェクトを保存する必要があります。
ここでの問題は、ClientInfo モデルに new Client() および new ClientProfile() オブジェクトを入力する必要があることです。私が持っているこのセットアップでそれを行うにはどうすればよいですか。