2011-01-06 54 views
1

我们使用Prism并从网格中弹出一个编辑窗体,它有两个选项,“Save”和“Save and New”。我的问题是关于重新初始化表单。我想知道是否有更好或更简单的方法?我要做的就是暴露在视图模型InteractionRequest,而且比使用InteractionRequestTrigger在XAML来改变窗体的属性,就像这样: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

一个干净的方式是在视图中摆脱逻辑的和将其保存在ViewModel中。

在XAML

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

在视图模型

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 

    } 
}