Silverlight:MVVM和重新初始化表单

Silverlight:MVVM和重新初始化表单,mvvm,silverlight-4.0,prism,Mvvm,Silverlight 4.0,Prism,我们使用Prism,从网格中弹出一个编辑表单,该表单有两个选项,“保存”和“保存并新建”。我的问题是关于重新初始化表单。我想知道是否有更好或更简单的方法?我要做的是在视图模型上公开一个InteractionRequest,然后在xaml中使用InteractionRequestTrigger来更改表单的属性,如下所示: private void SubmitAndNewCommandCallback(IEnumerable<ValidationResult> errors) {

我们使用Prism,从网格中弹出一个编辑表单,该表单有两个选项,“保存”和“保存并新建”。我的问题是关于重新初始化表单。我想知道是否有更好或更简单的方法?我要做的是在视图模型上公开一个InteractionRequest,然后在xaml中使用InteractionRequestTrigger来更改表单的属性,如下所示:

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>
private void SubmitAndNewCommandCallback(IEnumerable错误)
{
if(errors!=null&&errors.Any())
{
Errors=Errors.Select(x=>x.ErrorMessage.ToList();
}
其他的
{
如果(是新的)
{
_events.GetEvent().Publish(this.BidAgent);
}
_初始化FormRequest.Raise(此);
} 
}

一个干净的方法是去掉视图中的逻辑,并将其保留在ViewModel中

在xaml中

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

在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

    } 
}
private void SubmitAndNewCommandCallback(IEnumerable错误)
{
if(errors!=null&&errors.Any())
{
Errors=Errors.Select(x=>x.ErrorMessage.ToList();
}
其他的
{
如果(是新的)
{
_events.GetEvent().Publish(this.BidAgent);
}
SearchText=“”;
SelectedAgentType=AgentTypes.First();//选择第一个agenttype,或设置为null以在组合框中不选择任何内容
} 
}