none
IValueConverter InArgument<string> to string

    Question

  • I understand if you want to have a TextBox control or other WPF controls in an Activity Designer, to bind it to an actual property you will need a IValueConverter.

    Is there any of such converter available natively, like ArgumentToExpressionConverter? If not, how do you convert a value to InArgument and vice versa? For example if I have a TextBox and a string InArgument, I will need a ArgumentToStringConverter, don't I?

    Thursday, December 02, 2010 8:16 AM

Answers

  • can you have a try on this converter, it works on my machine with a custom activity having WPF textbox

     

    public class ArgumentToStringConverter : 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<string>))
                        {
                            convertedValue = (argumentModelItem.Properties["Expression"].ComputedValue as Literal<String>).Value;
                        }
                        else
                        {
                            convertedValue = (argumentModelItem.Properties["Expression"].ComputedValue as VisualBasicValue<string>).ExpressionText;
                        }
                    }
                }
                return convertedValue;
            }

     

            object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {

                // Convert string value to InArgument<string>

                string itemContent = (string)value;

                VisualBasicValue<string> vbArgument = new VisualBasicValue<string>(itemContent);

                InArgument<string> inArgument = new InArgument<string>(vbArgument);

                return inArgument;

            }


        }

    Friday, December 03, 2010 9:04 AM

All replies

  • for the converter from argument to wpf control, I think you need to write your own converter inherited from IValueConverter

    this is the sample

    http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

    • Proposed as answer by Jeff Cao Thursday, December 02, 2010 8:00 PM
    • Unproposed as answer by Louis Rhys Friday, December 03, 2010 2:49 AM
    Thursday, December 02, 2010 6:49 PM
  • yes I am aware of that. My question was, how to convert Inargument<string> to string, and vice versa
    Friday, December 03, 2010 2:49 AM
  • what do you mean by vice versa?
    Friday, December 03, 2010 3:42 AM
  • sorry, i mean from Inargument<string> to string AND from string to InArgument<String>. Because i will need both Convert and ConvertBack methods, right?
    Friday, December 03, 2010 6:59 AM
  • can you have a try on this converter, it works on my machine with a custom activity having WPF textbox

     

    public class ArgumentToStringConverter : 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<string>))
                        {
                            convertedValue = (argumentModelItem.Properties["Expression"].ComputedValue as Literal<String>).Value;
                        }
                        else
                        {
                            convertedValue = (argumentModelItem.Properties["Expression"].ComputedValue as VisualBasicValue<string>).ExpressionText;
                        }
                    }
                }
                return convertedValue;
            }

     

            object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {

                // Convert string value to InArgument<string>

                string itemContent = (string)value;

                VisualBasicValue<string> vbArgument = new VisualBasicValue<string>(itemContent);

                InArgument<string> inArgument = new InArgument<string>(vbArgument);

                return inArgument;

            }


        }

    Friday, December 03, 2010 9:04 AM
  • Hi, Louis

    Here is another sample,I created it one year ago, could be helpful to you:
    http://xhinker.com/2009/11/29/DotNetWF4UseTypeConverter.aspx

    Regards


    This posting is provided "AS IS" with no warranties, and confers no rights. Microsoft Online Community Support. My Blog:http://xhinker.com "Microsoft Windows Workflow Foundation 4.0 Cookbook"
    Monday, December 06, 2010 9:24 AM