locked
How to use plain WPF TextBox in Activity Designer instead of Expression box RRS feed

  • Question

  • I want users to have a simple text field to type into, without having to quote strings and worry about VB expressions. Trying to bind a text box in activity designer (see XAML below) does not save anything entered in the text box:

    <TextBox Text="{Binding Path=ModelItem.CustomArgument, Mode=TwoWay}"></TextBox>

    I searched for a long time but couldn't find resources describing how to do this. I am going to look into writing a custom converter next, using a "combo box converter" sample everyone keeps posting around. Using .NET 4.5 with VS 2012.


    • Edited by Valeriy Novytskyy Friday, December 28, 2012 1:30 AM Formatting came out wrong
    Friday, December 28, 2012 1:28 AM

Answers

  • Solved using a custom string converter. This was required to convert between InArgument<string> exposed by the custom code activity and a string Text property exposed by standard WPF TextBox control. Not too complicated, but unnecessary and over-engineered (hope Workflow team makes this easier in the future).

    Code for string converter (just insert as a new class into project that houses your custom activity designer):

    public class StringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ModelItem modelItem = value as ModelItem;
    
            if (value != null)
            {
                InArgument<string> inArgument = modelItem.GetCurrentValue() as InArgument<string>;
    
                if (inArgument != null && inArgument.Expression as Literal<string> != null)
                {
                    return (inArgument.Expression as Literal<string>).Value;
                }
            }
    
            return string.Empty;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return new InArgument<string>(new Literal<string>(value as string));
            }
    
            return null;
        }
    }

    This can be done the same way for a checkbox if you replace string with bool, and so on for other types of WPF controls.

    The way the convert is used in XAML is totally standard, other than not requiring a Parameter specified:

    ... xmlns:local="clr-namespace:ActivityDesignerLibrary1" ...

    <sap:ActivityDesigner.Resources>
     <local:StringConverter x:Key="strconv"/>
    </sap:ActivityDesigner.Resources>

    ...

    <TextBox Text="{Binding Path=ModelItem.CustomArgument, Mode=TwoWay, Converter={StaticResource strconv}}" Height="20"></TextBox>



    Friday, December 28, 2012 6:09 PM