0

我希望设置一个具有 2 个公共字符串属性的用户控件。该字符串应该是带有用户控件的页面控件的 ID。

在使用控件验证类作为一个示例以及标签的关联控件 ID 之后,我无法

  1. 将此控件添加到另一个页面时,将属性显示为属性面板中页面上的控件列表的下拉列表。(这是验证工作的控件的方式,试图找出我错过了什么。)
  2. 在我能够获得所有列表之后,我希望将其中一些限制为特定的控件类型(下拉列表或类似的东西)。根据我已经完成的一些额外阅读,我猜这需要使用自定义类型转换器来完成。

    [
        Category("Behavior"),
        DefaultValue(""),
        Description("The State Tex Box"),
        TypeConverterAttribute(typeof(AssociatedControlConverter))
    ]
    public string StateControlToAutoFill
    {
        get
        {
            object o = ViewState["StateControlToAutoFill"];
            return ((o == null) ? String.Empty : (string)o);
        }
        set
        {
            ViewState["StateControlToAutoFill"] = value;
        }
    }
    
4

1 回答 1

0

使用验证控件作为起点(验证道具的控件在很大程度上完成了我想要的操作)我能够通过以下方式解决这个问题。

  1. 看看ValidateControlConverter.cs
  2. 创建了我自己的 LabelOrTextBoxTypeConverter,并用我自己的逻辑覆盖了与 ValidateControlConverter (FilterControl) 相同的方法。

    class LabelOrTextBoxTypeConverter : ControlIDConverter
    {
    //public ControlByTypeIDConverter(List<Type> WantedControlTypes)
    //{
    //    this.ControlTypes = WantedControlTypes;
    //}
    /// <summary>
    /// 
    /// </summary>
    /// <param name="control"></param>
    /// <returns></returns>
    protected override bool FilterControl(Control control)
    {
        bool isWanted = false;
        foreach (var atype in this.ControlTypes)
        {
            isWanted |= control.GetType() == atype;
        }
    
        return isWanted;
    }
    
    
    public List<Type> ControlTypes { get { return new List<Type>() { typeof(TextBox), typeof(Label) }; } }
    

    }

  3. 最后一步是在控件属性上

    [
    Category("Target Controls")
    , DefaultValue("")
    , Bindable(true)
    , Description("The State Text Box To Auto Fill.")
    , TypeConverterAttribute(typeof(LabelOrTextBoxTypeConverter))
    ]
    public string StateControlToAutoFill
    

as you can see i didn't get 100% what i wanted here. i was hoping to set this up to just be a Control Filter class and have a list of the types of controls i was interested in passed in. I need to do a little more digging here to complete this but for the time being this is working as expected. anyone who knows the answer to this off the top of their head would be much appreciated.

All said and done this is working. Won't really get to use it to much as you have to be in design mode using the prop panel to take advantage of the generated list.

于 2013-04-22T18:38:33.493 回答