Can we create a Combobox for custom activity

I want have created this activitie
image
and I want to connect this properties with combo box


I think I’m missing smth in binding, in textbox work fine

Here is our binding in xaml code:

<ComboBox x:Name="cbEnvironment" HorizontalAlignment="Right" VerticalAlignment="Center" Width="150" Grid.Column="0" Margin="0,0,40,0" SelectedValue="{Binding ModelItem.KsefEnvironment, Mode=TwoWay}"/>

But we have also additional code for that in cs (cs for specific activity designer, where you have InitializeComponent method by default):

public InitSessionByTokenDesigner()
        {
            InitializeComponent();
            cbEnvironment.ItemsSource = LocalizedEnum<Environments>.GetLocalizedValues();
            cbEnvironment.DisplayMemberPath = nameof(LocalizedEnum.Name);
            cbEnvironment.SelectedValuePath = nameof(LocalizedEnum.Value);
        }

So the list is dynamically connected to enum (in my case - custom enum called Environment).

which parameter are in you code with value KsefEnviroment and LocalizedEnum
image

LocalizedEnum is the method form UiPath.Shared project.
add this import in file where you have InitializeComponent method:

using UiPath.Shared.Activities.Localization;

KsefEnviroment is our custom enum. We defined it in UiPath.Shared.Activities.Localization namespace to have direct access:

namespace UiPath.Shared.Activities.Localization
{
    public enum  Environments
    {
        [LocalizedDescription("TEST")]
        TEST,
        [LocalizedDescription("DEMO")]
        DEMO,
        [LocalizedDescription("PROD")]
        PROD
    }
}

This enum is used in .Activities project in activity code:

[LocalizedDisplayName(nameof(Resources.InitSessionByToken_Environment_DisplayName))]
[LocalizedDescription(nameof(Resources.InitSessionByToken_Environment_Description))]
[LocalizedCategory(nameof(Resources.Input_Category))]
public Environments KsefEnvironment { get; set; }

For the LocalizedEnum It still shows the same error



i have created the enum variable in localization
image
image
and for the part of properties it works fine

Now I just need to get this value from Environments and add it in ComboBox

Now I see you have have .net6 project.
My solution is in activity creator for visual studio 2019 and net461.
In localization.cs file I have these classes, which may not exist in your case:

public class LocalizedEnum
    {
        public string Name { get; private set; }
        public Enum Value { get; private set; }

        protected internal LocalizedEnum(string name, Enum value)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            Name = name;
            Value = value;
        }
    }

    public class LocalizedEnum<T> : LocalizedEnum
    {
        protected LocalizedEnum(string name, Enum value) : base(name, value)
        {
        }

        public static List<LocalizedEnum> GetLocalizedValues()
        {
            List<LocalizedEnum> localizedValues = new List<LocalizedEnum>();

            Type enumType = typeof(T);
            Array enumValues = Enum.GetValues(enumType);

            foreach (Enum value in enumValues)
            {
                string name = enumType.GetEnumName(value);
                FieldInfo field = enumType.GetField(name);
                DescriptionAttribute descriptionAttribute = field?.GetCustomAttribute<DescriptionAttribute>();

                localizedValues.Add(new LocalizedEnum(descriptionAttribute?.Description ?? name, value));
            }

            return localizedValues;
        }
    }

Try to paste these classes

1 Like

Thank you!
It works fine

Great, it was a nice fight :slight_smile:
I also recommend to see project UiPath Community Activities available in github:

There are many clever tricks to learn and this solution is perfect to improve skills in coding custom activities

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.