开发者

workflow 4 activity designer IValueConverter

开发者 https://www.devze.com 2023-02-04 01:09 出处:网络
Lets say i have an activity with InArgument<int> ProductId I\'d like to expose in the activity designer a combobox to show all Products and the user can select a product.

Lets say i have an activity with InArgument<int> ProductId

I'd like to expose in the activity designer a combobox to show all Products and the user can select a product.

I can show the list of product in the combo no problem. But how do I bind the selected product to the InArgument<int> of my custom activity?

I suppose I need some kind of ValueConverter? Not sure how to code the value converter for thi开发者_JS百科s case, if anybody has an idea, suggestion, will be helpful. I have to convert the InArgument<int> to an int? and the convert back from int to InArgument<int>

Thanks,


public class ArgumentToInt32Converter: IValueConverter {
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        object convertedValue = null;
        if (value != null) {
            ModelItem argumentModelItem = value as ModelItem;
            if (argumentModelItem != null && argumentModelItem.Properties["Expression"] != null && argumentModelItem.Properties["Expression"].Value != null) {
                if (argumentModelItem.Properties["Expression"].ComputedValue.GetType() == typeof(Literal <Int32> )) {
                    convertedValue = (argumentModelItem.Properties["Expression"].ComputedValue as Literal <Int32> ).Value;
                } else {
                    convertedValue = null
                }
            }
        }
        return convertedValue;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        // Convert Int32 value to InArgument<Int32>
        Int32 itemContent = (Int32) value;
        VisualBasicValue <Int32> vbArgument = new VisualBasicValue <Int32> (itemContent);
        InArgument <Int32> inArgument = new InArgument <Int32> (vbArgument);
        return inArgument;
    }
}

Modified from this answer


This is my attempt at making a more generic solution to this. I have several properties - some IEnumerable, some string, some int, and to make a value converter for each seems like the wrong approach. I'd be interested to know what cases I haven't caught here because I am relatively new to WF. Hopefully this helps someone.

public class ArgumentConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object convertedValue = null;
        if(value != null)
        {
            var argumentModelItem = value as ModelItem;

            if(argumentModelItem != null)
            {
                ModelProperty argumentModelProperty = argumentModelItem.Properties["Expression"];

                if(argumentModelProperty != null && argumentModelProperty.Value != null)
                {
                    var computedValue = argumentModelProperty.ComputedValue;

                    var activity = (Activity) computedValue;
                    convertedValue = WorkflowInvoker.Invoke(activity)["Result"];
                }
            }
        }
        return convertedValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // here targetType should be InArgument<T>
        // assume a single generic argument
        Type arg0 = targetType.GetGenericArguments()[0];

        ConstructorInfo argConstructor = targetType.GetConstructor(new[] {arg0});

        var argument = argConstructor.Invoke(new[] { value });

        return argument;
    }

    #endregion
}
0

精彩评论

暂无评论...
验证码 换一张
取 消