5

を使おうとしていますSystem.Windows.Forms.PropertyGrid

このグリッドでプロパティを非表示にするには、BrowsableAttributeset toを使用する必要がありますfalse。ただし、この属性を追加すると、プロパティはバインドできなくなります。

例:新しい Windows フォーム プロジェクトを作成し、 と を にドロップTextBoxPropertyGridますForm1。以下のコードを使用すると、 の幅はTextBoxにバインドされませんData.Width

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Data data = new Data();
        data.Text = "qwe";
        data.Width = 500;

        BindingSource bindingSource = new BindingSource();
        bindingSource.Add(data);

        textBox1.DataBindings.Add("Text", bindingSource, "Text", true,
            DataSourceUpdateMode.OnPropertyChanged);
        textBox1.DataBindings.Add("Width", bindingSource, "Width", true,
            DataSourceUpdateMode.OnPropertyChanged);

        propertyGrid1.SelectedObject = data;
    }
}

データ クラスのコードは次のとおりです。

public class Data : IBindableComponent
{
    public event EventHandler TextChanged;
    private string _Text;
    [Browsable(true)]
    public string Text
    {
        get
        {
            return _Text;
        }
        set
        {
            _Text = value;
            if (TextChanged != null)
                TextChanged(this, EventArgs.Empty);
        }
    }

    public event EventHandler WidthChanged;
    private int _Width;
    [Browsable(false)]
    public int Width
    {
        get
        {
            return _Width;
        }
        set
        {
            _Width = value;
            if (WidthChanged != null)
                WidthChanged(this, EventArgs.Empty);
        }
    }

    #region IBindableComponent Members

    private BindingContext _BindingContext;
    public BindingContext BindingContext
    {
        get
        {
            if (_BindingContext == null)
                _BindingContext = new BindingContext();

            return _BindingContext;
        }
        set
        {
            _BindingContext = value;
        }
    }

    private ControlBindingsCollection _DataBindings;
    public ControlBindingsCollection DataBindings
    {
        get 
        {
            if (_DataBindings == null)
                _DataBindings = new ControlBindingsCollection(this);

            return _DataBindings;    
        }
    }

    #endregion

    #region IComponent Members

    public event EventHandler Disposed;

    public System.ComponentModel.ISite Site
    {
        get
        {
            return null;
        }
        set
        {

        }
    }

    #endregion

    #region IDisposable Members

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    #endregion
}

Data のすべてのプロパティで Browsable 属性を true に切り替えると、機能します。現在、BindingSource は Browsable Attribute でデータソースを検索しているようです。

4

2 に答える 2

6

更新例に基づいて回答を更新しました:

Reflector を掘り下げてみたところ、「問題」が実際には にListBindingHelperあり、 によって使用されCurrencyManager、 によって使用されていることがわかりましたBindingSource(これらはすべてSystem.Windows.Forms名前空間にあります)。

バインド可能なプロパティを検出するために、 は をCurrencyManager呼び出しますListBindingSource.GetListItemProperties。内部的には、これはGetListItemPropertiesByInstance(単一のオブジェクトを渡すとき) を呼び出します。そのメソッドの最後に次のコード行があります。

return TypeDescriptor.GetProperties(target, BrowsableAttributeList);

そして、の実装BrowsableAttributeListは次のとおりです。

private static Attribute[] BrowsableAttributeList
{
    get
    {
        if (browsableAttribute == null)
        {
            browsableAttribute = new Attribute[] { new BrowsableAttribute(true) };
        }
        return browsableAttribute;
    }
}

実際には、 によってプロパティをフィルタリングしていることがわかりますBrowsableAttribute(true)。おそらくBindableAttribute代わりに使用する必要がありますが、デザイナーは、誰もがすでに依存していることに気づき、代わりにそれBrowsableAttributeを使用することにしたと思います.

残念ながら、 を使用すると、この問題を回避することはできませんBrowsableAttribute。唯一のオプションは、Marc が提案したことを実行して custom を使用するかTypeConverter、この質問のソリューションの 1 つを使用してプロパティ グリッド自体をフィルター処理することです: Programatically Hide Field in PropertyGrid

于 2010-01-12T18:34:23.930 に答える
3

BrowsableAttribute含まれないようにするためのメカニズムとして、多くのコンポーネントモデルの方法で使用されます。おそらく最良の選択肢は、追加しないこと[Browsable(false)]です。

PropertyGridフィルタリングには他にもいくつかの方法がありますが、最も簡単なのはおそらく、必要なものを説明する属性を伝え( ) 、TypeConverterその他のプロパティをマークすることです。ICustomTypeDescriptorTypeDescriptionProviderPropertyGrid.BrowsableAttributes

別のオプションはカスタムタブを作成することです-しかし、それは私の経験ではヒットアンドミスです.

これは で使用されますが、他のほとんどのバインディングTypeConverterでは使用されPropertyGridません。特定のプロパティを名前で除外するカスタム型コンバーターを使用することで機能Attribute.IsDefinedしますが、マスキングを行うようなものを使用することもできます。

using System.Windows.Forms;
using System;
using System.Linq;
using System.ComponentModel;
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Data data = new Data { Name = "the name", Value = "the value" };
        using (Form form = new Form
        {
            Controls =
            {
                new PropertyGrid {
                    Dock = DockStyle.Fill,
                    SelectedObject = data
                },
                new TextBox {
                    Dock = DockStyle.Bottom,
                    DataBindings = { {"Text", data, "Value"}, }
                },
                new TextBox {
                    Dock = DockStyle.Bottom,
                    DataBindings = { {"Text", data, "Name"}, }
                }
            }
        })
        {
            Application.Run(form);
        }        
    }
}
[TypeConverter(typeof(DataConverter))]
class Data
{
    class DataConverter : ExpandableObjectConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            var props = base.GetProperties(context, value, attributes);
            return new PropertyDescriptorCollection(
                (from PropertyDescriptor prop in props
                 where prop.Name != "Value"
                 select prop).ToArray(), true);
        }
    }
    public string Value { get; set; }
    public string Name { get; set; }
}
于 2010-01-12T21:06:49.177 に答える