c# usercontrol 设置自定义属性的选项列表

标签 c# winforms user-controls custom-controls property-editor

我正在尝试创建一个自定义可折叠面板以用于我的一个项目。我将它设置为显示哪些边应该能够折叠,它需要一个字符串输入 {"None", "Up", "Right", "Down", "Left", "All"};

这是我目前所拥有的:

public partial class CollapsiblePanel : UserControl
{
    # region Collapse Direction
    List<string> DirectionOptions = new List<string> { "None", "Up", "Right", "Down", "Left", "All" };
    [Browsable(true), DefaultValue("All"), Description("Direction panel collapses. 0-none, 1-up, 2-right, 3-down, 4-left, 5-all")]
    [ListBindable(true), Editor(typeof(ComboBox), typeof(UITypeEditor))]
    private string _direction = "All";
    public List<string> Direction
    {
        get { return DirectionOptions; }
        set
        {
            DirectionOptions = value;
            callCollapseDirectionChanged();
        }
    }

    public event Action CollapseDirectionChanged;
    protected void callCollapseDirectionChanged()
    {
        Action handler = CollapseDirectionChanged;
        DisplayButtons();
        if (handler != null)
        {
            handler();
        }
    }
    # endregion

    public CollapsiblePanel()
    {
        InitializeComponent();
    }

    private void DisplayButtons()
    {
        switch (_direction)
        {
            case "None":
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case "Up":
                buttonDown.Visible = false;
                buttonUp.Visible = true;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case "Right":
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = true;
                buttonLeft.Visible = false;
                break;

            case "Down":
                buttonDown.Visible = true;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case "Left":
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = true;
                break;

            case "All":
                buttonDown.Visible = true;
                buttonUp.Visible = true;
                buttonRight.Visible = true;
                buttonLeft.Visible = true;
                break;
        }
    }
}

有人可以向我解释如何让设计者为用户提供 DirectionOptions 列表作为可能的值吗?他们可以选择任何一个字符串作为值。

最佳答案

用户应该能够选择多个选项还是只能选择一个?

如果只有一个 - 那么该属性应该是枚举而不是列表。

像这样的东西应该可以工作:

public partial class CollapsiblePanel : UserControl
{
    public enum CollapseDirection
    {
        None,
        Up,
        Right,
        Down,
        Left,
        All
    }

    # region Collapse Direction

    [Browsable(true), DefaultValue("All"), Description("Direction panel collapses. 0-none, 1-up, 2-right, 3-down, 4-left, 5-all")]
    [ListBindable(true), Editor(typeof(ComboBox), typeof(UITypeEditor))]
    private CollapseDirection _direction = CollapseDirection.All;
    public CollapseDirection Direction
    {
        get { return _direction; }
        set
        {
            _direction = value;
            callCollapseDirectionChanged();
        }
    }

    public event Action CollapseDirectionChanged;
    protected void callCollapseDirectionChanged()
    {
        Action handler = CollapseDirectionChanged;
        DisplayButtons();
        if (handler != null)
        {
            handler();
        }
    }
    # endregion

    public CollapsiblePanel()
    {
        InitializeComponent();
    }

    private void DisplayButtons()
    {
        switch (_direction)
        {
            case CollapseDirection.None:
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case CollapseDirection.Up:
                buttonDown.Visible = false;
                buttonUp.Visible = true;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case CollapseDirection.Right:
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = true;
                buttonLeft.Visible = false;
                break;

            case CollapseDirection.Down:
                buttonDown.Visible = true;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = false;
                break;

            case CollapseDirection.Left:
                buttonDown.Visible = false;
                buttonUp.Visible = false;
                buttonRight.Visible = false;
                buttonLeft.Visible = true;
                break;

            case CollapseDirection.All:
                buttonDown.Visible = true;
                buttonUp.Visible = true;
                buttonRight.Visible = true;
                buttonLeft.Visible = true;
                break;
        }
    }
}
  • 注意 - 这是指南 - 我还没有测试代码,但这应该让你了解你想要实现的目标的大体方向。

关于c# usercontrol 设置自定义属性的选项列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21917711/

相关文章:

wpf - 当转换器位于 UserControl.Resources 中时,XAML 设计器 "cannot find type"

c# - 将遗留代码库从 VB 转换为 C# 是否有任何值(value)?

c# - 如何将枚举绑定(bind)到组合框

WPF 将 ICommand 绑定(bind)到事件 (FrameworkElement.Unloaded)

vba - 用户窗体在 "End Sub"之后关闭,而无需调用 "Unload Me"

c# - winform 中的 OnPaint - .NET Compact Framework 3.5

c# - 尝试捕捉错误的形式?

c# - 如何使用 DryIoc 而不是 Unity 在 Prism 中配置模块目录?

javascript - 从服务器端将字符串传递给 JavaScript 函数

mysql - Visual Basic MySQL Datagrid 从数据库中删除