Dynamically binding the expression type in the property grid

Previously, I demonstrated how to use an ExpressionTextBox in a custom property editor. In this followup post, I will show how to dynamically bind the ExpressionType of the ExpressionTextBox to the type of the argument of the custom activity. We don’t provide a type converter for this out of the box, so you have to write your own. Fortunately this is not hard.

Here’s the type converter:

 using System;
using System.Windows.Data;
using System.Globalization;

namespace Microsoft.CustomTypesAndActivities
{
    public class TypeToArgumentTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object convertedValue = null;
            Type type = value as Type;
            if (type != null)
            {
                if (type.GetGenericArguments().Length > 0)
                {
                    convertedValue = type.GetGenericArguments()[0];
                }
            }
            return convertedValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Now you can go ahead and use this in your property editor. In this case, the ct prefix points to the assembly with the type converter.

 <sap:ActivityDesigner.Resources>
        <sapc:ArgumentToExpressionModelItemConverter  x:Key="ArgumentToExpressionModelItemConverter" />
        <sapc:ModelPropertyEntryToOwnerActivityConverter x:Key="ModelPropertyEntryToOwnerActivityConverter" />
        <ct:TypeToArgumentTypeConverter x:Key="TypeToArgumentTypeConverter" />
 
        
        <DataTemplate x:Key="MyDataTemplate">
                <StackPanel >
                <sapv:ExpressionTextBox 
                ExpressionType="{Binding Path=ParentProperty.PropertyType, Converter={StaticResource TypeToArgumentTypeConverter}}"
                OwnerActivity="{Binding Path=ParentProperty, Converter={StaticResource ResourceKey=ModelPropertyEntryToOwnerActivityConverter}, Mode=OneWay}"                               
                Background="Pink"
                >
                    <sapv:ExpressionTextBox.Expression>
                        <MultiBinding  Mode="TwoWay"  Converter="{StaticResource ResourceKey= ArgumentToExpressionModelItemConverter}" ConverterParameter="In" >
                            <Binding Path="Value" Mode="TwoWay"/>
                            <Binding Path="ParentProperty" Mode="OneWay" />
                        </MultiBinding>
                    </sapv:ExpressionTextBox.Expression>
                </sapv:ExpressionTextBox>
            </StackPanel>
        </DataTemplate>

    </sap:ActivityDesigner.Resources>

Obviously yours will not be pink but that’s just to show that yes, our template does indeed work. And there you have it, everything you ever wanted to know about type converters but were afraid to ask. For good measure, I am attaching some sample code with a fully functional custom property editor. Have fun.

ETBCustomTypesRehost.zip