2011-01-06 53 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 

    } 
}