Silverlight:MVVM和重新初始化表单

时间:2011-01-06 14:23:00

标签: mvvm silverlight-4.0 prism

我们使用Prism,我们会从网格中弹出一个编辑表单,其中包含两个选项“Save”和“Save and New”。我的问题是关于重新初始化表单。我想知道是否有更好或更简单的方法?我所做的是在视图模型上公开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>

1 个答案:

答案 0 :(得分:0)

一种干净的方法是摆脱视图中的逻辑并将其保存在ViewModel中。

in 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

    } 
}