Sto provando a usare System.Windows.Forms.PropertyGrid
.Perché l'attributo Sfogliabile rende la proprietà non associabile?
Per rendere una proprietà non visibile in questa griglia, è necessario utilizzare BrowsableAttribute
impostato su false
. Ma l'aggiunta di questo attributo rende la proprietà non associabile.
Esempio: Creare un nuovo progetto Windows Form, e rilasciare un TextBox
e PropertyGrid
su Form1
. Usando il codice qui sotto, la larghezza del TextBox
non viene tenuto a 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;
}
}
Il codice per la classe di dati è:
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
}
Se si cambia l'attributo sfogliabile su true su ogni proprietà in Data funziona. Ora sembra che BindingSource cerchi l'origine dati per l'attributo Browsable.
Sì, hai ragione. Sembra funzionare. Ho questo problema in un grande progetto. Proverò a scrivere un esempio migliore al più presto. – bodziec