【问题标题】:Silverlight: MVVM and re-initializing formSilverlight:MVVM 和重新初始化表单
【发布时间】:2011-06-04 16:37:37
【问题描述】:

我们使用 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>

【问题讨论】:

    标签: mvvm silverlight-4.0 prism


    【解决方案1】:

    一种干净的方法是去掉 View 中的逻辑并将其保留在 ViewModel 中。

    在xml中

    <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
    
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-01
      • 2019-12-05
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2011-03-13
      • 2015-07-18
      相关资源
      最近更新 更多