【问题标题】:How to configure ExpressionTextBox bindings / OwnerActivity when used in a dialog?在对话框中使用时如何配置 ExpressionTextBox 绑定/ OwnerActivity?
【发布时间】:2015-08-25 20:16:03
【问题描述】:

我们的小组正在围绕我们的电子邮件活动开发自定义活动设计器。这是一个非常直接的设计器,允许用户输入设置/凭据,但我们没有将所有可设置的选项弄乱活动设计器,而是考虑将一些设置放在对话框窗口中。 (当您单击服务器地址框旁边的按钮时打开)。

我们的一些电子邮件活动属性是 InArguments,因此我们尝试使用 ExpressionTextBox 来显示这些值,但运气不佳。主要问题是我们不确定如何在 ExpressionTextBox 上正确设置绑定和 OwnerActivity。在 Activity Designer 的 xaml 中,这只是通过使用 InArgument 的转换器设置 Expression=ModelItem.Property 并设置 OwnerActivity=ModelItem 来完成,如下所示:

<view:ExpressionTextBox HintText="Enter a VB Expression" Expression="{Binding ModelItem.ServerAddress, ConverterParameter=In, Converter={StaticResource ArgumentToExpressionConverter}, Mode=TwoWay}" ExpressionType="{x:Type system:String}" OwnerActivity="{Binding ModelItem}" Margin="2" MaxLines="1" />

如果有人对我们如何在对话中完成此任务有任何想法,请提出建议。

【问题讨论】:

    标签: workflow-foundation-4


    【解决方案1】:

    嗯,这确实是一个 WPF\MVVM 问题,而不是 WF4。

    在开发自定义活动设计器时,您只需记住一件事:对设计器\对话框所做的任何更改都应反映在 ModelItem 通过 XAML绑定表达式或通过ModelItem.Properties 属性上的代码。

    现在,您何时以及如何做,有几个答案,但这实际上是一个实现细节,取决于您想要如何做。

    假设您在 button-beside-the-server-address-box 点击时显示对话框。并且我们还假设您可以通过它们的名称访问对话框文本框。此时,您可以访问ModelItem,因此只需根据需要设置其属性:

    private void ButtonNextToServerAddressBox_OnClick(object sender, RoutedEventArgs e) 
    {
        var dialog = new ServerAddressEditor();
        var result = dialog.ShowDialog();
    
        if (result ?? false) 
        {
            ModelItem.Properties["Server"].SetValue(new InArgument<string>(dialog.ServerTextBox.Text));
            ModelItem.Properties["Port"].SetValue(new InArgument<string>(dialog.PortTextBox.Text));
            // ... set all other properties
        }
    }
    

    现在,如果您正在使用任何其他模式,或者您想要纯 MVVM,由于 ModelItem 的工作原理,它可能会更加棘手。但这是一个非常好的方法。

    【讨论】:

    • 我们知道如何设置 ModelItem 的属性。那不是问题。问题是当没有直接在活动设计器上使用时,如何(如果可能)设置 ExpressionTextbox 的绑定。
    【解决方案2】:

    我通过在对话框的 ViewModel 中创建一个属性来保存活动设计器的 ModelItem 解决了这个问题。

    public ModelItem OwnerActivity {
        get { return _OwnerActivity; }
        set { _OwnerActivity = value; }
    }
    
    vm.OwnerActivity = this.DataContext.ModelItem;
    

    然后我将对话框中的表达式文本框的 Xaml 设置为绑定到此:

    <view:ExpressionTextBox HintText="Enter a VB Expression" Expression="
        {Binding Path=OwnerActivity.ServerAddress, ConverterParameter=In, Converter=
        {StaticResource ArgumentToExpressionConverter}, Mode=TwoWay}" ExpressionType="
        {x:Type system:String}" OwnerActivity="{Binding OwnerActivity}" Margin="2" 
        MaxLines="1" />
    

    因为我现在直接从活动设计器绑定到 ModelItem,所以始终提交从对话框中对 ModelItem 属性所做的任何更改,即使您从对话框中选择取消也是如此。为了连接确定/取消按钮以便它们相应地工作,我在对话框中执行了以下操作:

    // declare a ModelEditingScope to make changes transactional
    private ModelEditingScope _editScope;
    
    // add this to the constructor of the dialog to begin transactional edits on the ModelItem 
    _editScope = editorViewModel.OwnerActivity.BeginEdit();
    
    // ok & cancel button click event to commit or revert the changes.
    private void OK_Click(object sender, RoutedEventArgs e)
    {
        _editScope.Complete();
        this.DialogResult = DialogResult.OK;
        this.Close();
    }    
    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        _editScope.Revert();
        this.DialogResult = DialogResult.Cancel;
        this.Close()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 2014-07-02
      • 1970-01-01
      相关资源
      最近更新 更多