XCAD.NET
XCAD.NET
Framework for .NET (C# and VB.NET) to create modern application for CAD systems (SOLIDWORKS, SOLIDWORKS Document Manager, Autodesk Inventor, etc.)
Get technical support Connect to xCAD community on Discord server Explore code and application examples xCAD.NET Templates for Visual Studio and VS Code Access source code

Combo Box control in SOLIDWORKS property Manager Page


Combo Box control with 3 options
Combo Box control with 3 options

Combo box control will be automatically generated for all the properties of enumerator types. All values of enumerators will be considered as the items in the combo box:

public enum Options_e
{
    Option1,
    Option2,
    Option3
}

[ComboBoxOptions(ComboBoxStyle_e.Sorted)]
public Options_e Options { get; set; }

Additional options and style for combo box control can be specified via ComboBoxOptionsAttribute

Item Text

TitleAttribute attribute can be used to specify user friendly title for the items to be shown in the combo box

public enum OptionsCustomized_e
{
    [Title("First Option")] //static title
    Option1,

    [Title(typeof(Resources), nameof(Resources.Option2Title))] //title loaded from resources
    Option2
}

public OptionsCustomized_e Options2 { get; set; }

Dynamic Items Provider

In some cases items might need to be composed at runtime. In order to dynamically assign the list of item to combobox decorate the property with CustomItemsAttribute, create a type which implements ICustomItemsProvider (or SwCustomItemsProvider for specific SOLIDWORKS implementation) and pass the type to attribute.

Framework will call the provider to resolve the items. Make sure that the type of the target property matches the type of values returned from the provider.

When returning custom class from ProvideItems override ToString method to provide display name for the item in the combo box.

public class CustomStringItemsProvider : SwCustomItemsProvider<string>
{
    public override IEnumerable<string> ProvideItems(ISwApplication app, IControl[] dependencies)
        => new string[] { "A", "B", "C" };
}

public class CustomIntItemsProvider : SwCustomItemsProvider<int>
{
    public override IEnumerable<int> ProvideItems(ISwApplication app, IControl[] dependencies)
        => new int[] { 1, 2, 3 };
}

[ComboBox(typeof(CustomStringItemsProvider))]
[ControlTag(nameof(Options3))]
public string Options3 { get; set; } = "B";

[ComboBox(typeof(CustomIntItemsProvider))]
[ControlTag(nameof(Options4))]
public int Options4 { get; set; }

In order to assign the control dependencies (i.e. controls which affect the list of values in the combobox), provide the corresponding control tags in the second parameter of CustomItemsAttribute. IN this case the ProvideItems method will be called when values of parent control change. In this case controls wil lbe passed as dependencies parameter of ProvideItems.

Note. dependencies parameter of ProvideItems method will contain null items for the first rendering of the control before the binding is done. This method will be called again once binding is resolved with correct controls.

public class CustomComboBoxItem
{
    public string BaseName { get; }
    public string Name { get; }

    public CustomComboBoxItem(object dep1, object dep2, string name)
    {
        BaseName = $"{dep1}_{dep2}";
        Name = name;
    }

    public override string ToString() => $"Item-{BaseName}-{Name}";
}

public class CustomDependencyProvider : SwCustomItemsProvider<CustomComboBoxItem>
{
    public override IEnumerable<CustomComboBoxItem> ProvideItems(ISwApplication app,
        IControl[] dependencies)
    {
        return new CustomComboBoxItem[]
        {
            new CustomComboBoxItem(dependencies[0]?.GetValue(), dependencies[1]?.GetValue(), "I1"),
            new CustomComboBoxItem(dependencies[0]?.GetValue(), dependencies[1]?.GetValue(), "I2"),
            new CustomComboBoxItem(dependencies[0]?.GetValue(), dependencies[1]?.GetValue(), "I3")
        };
    }
}

[ComboBox(typeof(CustomDependencyProvider), nameof(Options3), nameof(Options4))]
public CustomComboBoxItem Options5 { get; set; }

Refer Weldment Profiles Selector example which demonstrates how to create dynamic cascading combo boxes.


Powered by Docify