2011-01-06 15 views
1

私たちはPrismを使用し、グリッドから「保存」と「保存して新規」という2つのオプションを持つ編集フォームをポップアップします。私の質問は、フォームを再初期化することです。もっと良い方法や簡単な方法があるのだろうか?Silverlight:MVVMと再初期化フォーム

private void SubmitAndNewCommandCallback(IEnumerable<ValidationResult> errors) 
{ 
    if (errors != null && errors.Any()) 
    { 
     Errors = errors.Select(x => x.ErrorMessage).ToList(); 
    } 
    else 
    { 
     if (IsNew) 
     { 
      _events.GetEvent<BidAgentCreated>().Publish(this.BidAgent); 
     } 

     _intializeFormRequest.Raise(this); 
    } 
} 


<i:Interaction.Triggers> 
    <prism:InteractionRequestTrigger SourceObject="{Binding InitializeFormRequest}" > 
     <ei:ChangePropertyAction TargetName="ctlAgentType" PropertyName="SelectedIndex" Value="0" /> 
     <ei:ChangePropertyAction TargetName="ctlAgentSearchBox" PropertyName="Text" Value=""/> 
    </prism:InteractionRequestTrigger> 
</i:Interaction.Triggers> 

答えて

0

きれいな方法は、あなたのビューにロジックを取り除くことです:私は何をすると、ビューモデルにInteractionRequestを公開し、フォームのプロパティを変更するにはXAMLでInteractionRequestTriggerを使用するよりも、このようなものですViewModelに保持します。 ViewModelに

private void SubmitAndNewCommandCallback(IEnumerable<ValidationResult> errors) 
{ 
    if (errors != null && errors.Any()) 
    { 
     Errors = errors.Select(x => x.ErrorMessage).ToList(); 
    } 
    else 
    { 
     if (IsNew) 
     { 
      _events.GetEvent<BidAgentCreated>().Publish(this.BidAgent); 
     } 

     SearchText=""; 
     SelectedAgentType = AgentTypes.First(); //selects first agenttype, or set to null to select nothing in the combobox 

    } 
} 

XAMLで

<ComboBox ItemsSource="{Binding AgentTypes}" SelectedItem="{Binding SelectedAgentType,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/> 
<TextBox Text="{Binding SearchText,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />