将数据绑定到自定义活动设计器中的Combobox

Chr*_*lor 10 workflow workflow-foundation workflow-foundation-4

我有一个自定义活动,其中一个参数是一个字符串.但是,我不希望设计人员输入任意字符串,而是希望向设计人员提供带有选项列表的Combobox,这些选项是动态的,并从数据库加载到List <>集合中.

我的问题是我不知道如何将设计器中的Combobox绑定到此列表并将选择设置为活动的in参数.在视觉上我让活动设计师工作,这只是一步.

Tim*_*ith 7

通常情况下,我会用a property而不是a来编写活动InArgument.这简化了方案:

<ComboBox ItemsSource="{Binding Path=ValidOptions}" 
 SelectedValue="{Binding Path=ModelItem.MyStringProperty, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)

(这里ValidOptions是ActivityDesigner类的一些Collection属性.MyStringProperty是底层活动的一些公共get/set/property,例如:

public string MyStringProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)

)

如果你加,你将有问题InArgument的组合是从组合框的字符串值不能直接分配给ModelItem期待InArgument<string>.这可以使用IValueConverter绑定中的自定义来修复.


sfu*_*qua 5

以前的答案很有帮助,但对我来说还不够。最终,我在 Microsoft 的 .Net 4.5 开发人员指南中找到了 2012 年的一篇很棒的文章:将自定义活动属性绑定到设计器控件。那篇文章几乎是完整的答案 - 除了自定义转换器类中的一个小错误和一个主要缺陷:该技术将从 ComboBox 保存一个值,但在您重新打开工作流程时不会恢复它。

微软的 Ron Jacobs为自定义活动设计师提供了另一个答案。我最终将两者结合起来以获得一个可行的解决方案。

定制设计师

ModelToObjectValueConverter是一个非常有用的资源,让我可以跳过创建自己的IValueConverter. 在ObjectDataProvider你看到我通过调用静态方法加载一个字符串列表,People.GetPeople(). ComboBox 绑定到该提供程序作为项目源,但将所选值绑定到自定义 Activity 上的 Person 属性(如下)

<sap:ActivityDesigner x:Class="ActivityLibrary1.ComboBoxActivityDesigner"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
    xmlns:sapc="clr-namespace:System.Activities.Presentation.Converters;assembly=System.Activities.Presentation"
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
    xmlns:c="clr-namespace:ActivityLibrary1">

    <sap:ActivityDesigner.Resources>
        <ResourceDictionary>
            <sapc:ModelToObjectValueConverter x:Key="ModelToObjectValueConverter" />
            <ObjectDataProvider x:Key="people" ObjectType="{x:Type c:People}" MethodName="GetPeople"/>
        </ResourceDictionary>
    </sap:ActivityDesigner.Resources>

    <Grid>
        <Label Content="Person" HorizontalAlignment="Left" VerticalAlignment="Top" />
        <ComboBox HorizontalAlignment="Left" 
                  Margin="66,0,0,0" 
                  VerticalAlignment="Top" 
                  Width="120"
                  SelectedValue="{Binding Path=ModelItem.Person, Mode=TwoWay, Converter={StaticResource ModelToObjectValueConverter} }"
                  ItemsSource="{Binding Source={StaticResource people}}">
        </ComboBox>
    </Grid>
</sap:ActivityDesigner>
Run Code Online (Sandbox Code Playgroud)

自定义代码活动

请注意,这使用属性而不是 InArgument,这使得绑定 ComboBox 更容易。

[Designer(typeof(ComboBoxActivityDesigner))]
public class CodeActivity1 : CodeActivity 
{      
    public string Person { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        // Just to demonstrate that it worked
        MessageBox.Show(Person);    
    }
}
Run Code Online (Sandbox Code Playgroud)

工作流程

现在可以将自定义活动CodeActivity1拖到工作流上。当您进行选择时,所选值将出现在属性窗格中。保存工作流程。关闭并重新打开。先前选择的值将根据需要保留。

自定义活动设计器的屏幕截图