locked
How to make ToolboxControl to show tooltips for custom activities? RRS feed

  • Question

  • With .NET 4, I have a Workflow designer in a rehosted application.  According to this other post: http://social.msdn.microsoft.com/Forums/en-CA/wfprerelease/thread/3d97dc38-730b-4331-9155-58d1c11e5614

    It seems like setting a Description attribute to the custom activity would enable the tooltips on ToolboxControl when user mouse hovering the custom activity.  However, I cannot make it happen.  Here is the code.  Please let me know if I am missing something. Note that the Bitmap set in ListComponentToolBoxItem is shown on the ToolboxControl correctly.

    [Serializable]
    internal class ListComponentToolBoxItem : ToolboxItem
    {
        public ListComponentToolBoxItem() : base()
        {
            Description = "Here is the description that I would show to user as tooltip.";
            Bitmap = new System.Drawing.Bitmap("c:\\2.bmp");
        }
    }

    [ToolboxItem(typeof(ListComponentToolBoxItem))]
    public sealed class ListComponent : NativeActivity
    { ... }

    And the main code that put up the ToolboxControl:

    Window win = new Window();
    var tbc = new System.Activities.Presentation.Toolbox.ToolboxControl();
    var tbCategory = new System.Activities.Presentation.Toolbox.ToolboxCategory("My Custom Activities");
    tbCategory.Add(new System.Activities.Presentation.Toolbox.ToolboxItemWrapper(typeof(ListComponent), "List Activity"));
    tbc.Categories.Add(tbCategory);

    win.Content = tbc;
    win.ShowDialog();

    Thank you very much.

    Tuesday, April 27, 2010 3:43 AM

Answers

  •  

    Hi Lee,

    I think you may not use ToolboxItemWrapper correctly. The ToolboxItemWrapper does not support pass ToolboxItem as its toolType argument. You should pass Activity as its toolType argument. Like this:

    var itemWrapper1 = new ToolboxItemWrapper(typeof(Assign));

    Then when user drag&drop the item from rehost toolbox, an Assign will be added to Designer surface. The [ToolboxItem()] attribute is only supported by VS, I think.

    Then you would like to know how could tooltip be added to rehost toolboxitem? Here is an example for this. As we know, ToolboxItemWrapper class do not has a ToolTip property for user to set its value. We can only change ToolboxItemStyle in ToolboxControl, use Setter to set the ToolTip Property.

    <my:ToolboxControl HorizontalAlignment="Left" Margin="186,134,0,0" Name="toolboxControl" VerticalAlignment="Top" >

        <my:ToolboxControl.ToolItemStyle>

            <Style TargetType="{x:Type FrameworkElement}">

                <Setter Property="ToolTip" Value="{Binding Converter={StaticResource toolTipConvertor}}" />

            </Style>               

        </my:ToolboxControl.ToolItemStyle>

    </my:ToolboxControl>

    What does the toolTipConvertor stand for? Here is the source code for it. The convertor has an internal Dictionary which stores the mapping of a ToolboxItemWrapper and its tooltip.

    [ValueConversion(typeof(ToolboxItemWrapper), typeof(string))]

    public class ToolTipConvertor : IValueConverter

    {

        public static Dictionary<ToolboxItemWrapper, string> ToolTipDic = new Dictionary<ToolboxItemWrapper, string>();

     

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            ToolboxItemWrapper itemWrapper = (ToolboxItemWrapper)value;

            if (ToolTipDic.ContainsKey(itemWrapper))

            {

                return ToolTipDic[itemWrapper];

            }

            else

               return null;

        }

     

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

        {

            return null;

        }

    }

    The last step is create the relationship between a ToolboxItemWrapper and the tooltip:

    var category = new ToolboxCategory("Category");

     

    var itemWrapper1 = new ToolboxItemWrapper(typeof(Assign));           

    ToolTipConvertor.ToolTipDic.Add(itemWrapper1, "Tooltip for Assign");

    category.Add(itemWrapper1);

     

    var itemWrapper2 = new ToolboxItemWrapper(typeof(WriteLine));

    ToolTipConvertor.ToolTipDic.Add(itemWrapper2, "Tooltip for WriteLine");

    category.Add(itemWrapper2);

     

    toolboxControl.Categories.Add(category);

    If you want to know more about customizing the ToolboxItemWrapper, here is a good post by tilovell:

    http://blogs.msdn.com/tilovell/archive/2010/03/08/the-workflow-4-0-custom-activity-icon-and-toolbox-icon-faq.aspx

    If you want the project, ping me at: yuawan at microsoft dot com

    Thanks,
    Yuanji Wang

     

     

     


    This posting is provided "AS IS" and confers no rights or warranties.
    • Marked as answer by LLee Friday, April 30, 2010 10:27 AM
    Wednesday, April 28, 2010 3:18 AM

All replies

  •  

    Hi Lee,

    I think you may not use ToolboxItemWrapper correctly. The ToolboxItemWrapper does not support pass ToolboxItem as its toolType argument. You should pass Activity as its toolType argument. Like this:

    var itemWrapper1 = new ToolboxItemWrapper(typeof(Assign));

    Then when user drag&drop the item from rehost toolbox, an Assign will be added to Designer surface. The [ToolboxItem()] attribute is only supported by VS, I think.

    Then you would like to know how could tooltip be added to rehost toolboxitem? Here is an example for this. As we know, ToolboxItemWrapper class do not has a ToolTip property for user to set its value. We can only change ToolboxItemStyle in ToolboxControl, use Setter to set the ToolTip Property.

    <my:ToolboxControl HorizontalAlignment="Left" Margin="186,134,0,0" Name="toolboxControl" VerticalAlignment="Top" >

        <my:ToolboxControl.ToolItemStyle>

            <Style TargetType="{x:Type FrameworkElement}">

                <Setter Property="ToolTip" Value="{Binding Converter={StaticResource toolTipConvertor}}" />

            </Style>               

        </my:ToolboxControl.ToolItemStyle>

    </my:ToolboxControl>

    What does the toolTipConvertor stand for? Here is the source code for it. The convertor has an internal Dictionary which stores the mapping of a ToolboxItemWrapper and its tooltip.

    [ValueConversion(typeof(ToolboxItemWrapper), typeof(string))]

    public class ToolTipConvertor : IValueConverter

    {

        public static Dictionary<ToolboxItemWrapper, string> ToolTipDic = new Dictionary<ToolboxItemWrapper, string>();

     

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            ToolboxItemWrapper itemWrapper = (ToolboxItemWrapper)value;

            if (ToolTipDic.ContainsKey(itemWrapper))

            {

                return ToolTipDic[itemWrapper];

            }

            else

               return null;

        }

     

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

        {

            return null;

        }

    }

    The last step is create the relationship between a ToolboxItemWrapper and the tooltip:

    var category = new ToolboxCategory("Category");

     

    var itemWrapper1 = new ToolboxItemWrapper(typeof(Assign));           

    ToolTipConvertor.ToolTipDic.Add(itemWrapper1, "Tooltip for Assign");

    category.Add(itemWrapper1);

     

    var itemWrapper2 = new ToolboxItemWrapper(typeof(WriteLine));

    ToolTipConvertor.ToolTipDic.Add(itemWrapper2, "Tooltip for WriteLine");

    category.Add(itemWrapper2);

     

    toolboxControl.Categories.Add(category);

    If you want to know more about customizing the ToolboxItemWrapper, here is a good post by tilovell:

    http://blogs.msdn.com/tilovell/archive/2010/03/08/the-workflow-4-0-custom-activity-icon-and-toolbox-icon-faq.aspx

    If you want the project, ping me at: yuawan at microsoft dot com

    Thanks,
    Yuanji Wang

     

     

     


    This posting is provided "AS IS" and confers no rights or warranties.
    • Marked as answer by LLee Friday, April 30, 2010 10:27 AM
    Wednesday, April 28, 2010 3:18 AM
  • Yuanji,

    Thank you very much.  It works!

    Cheers.

    Friday, April 30, 2010 10:27 AM