Liaison de données ApplicationSettings aux composants personnalisés
-
09-06-2019 - |
Question
J'ai un composant personnalisé dans lequel j'ai implémenté
INotifyPropertyChanged
et IBindableComponent
.
Cependant, lorsque j'essaie de rechercher une propriété dans une donnée, le concepteur ajoute ceci ligne:
this.component11.TestString =
global::WindowsFormsApplication2.Properties.Settings.Default.Setting;
au lieu de créer une liaison comme avec une zone de texte:
this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding(
"Text",
global::WindowsFormsApplication2.Properties.Settings.Default,
"Setting2",
true,
System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
J'aurais pensé que le concepteur regarderait simplement pour voir si
IBindableComponent
est implémenté et si c'est le cas, générez la liaison.
codage au lieu du code d'attribution.
Avez-vous des idées sur la raison pour laquelle cela fonctionne avec une zone de texte et non avec mon composant personnalisé?
Voici mon composant personnalisé:
public partial class Component1 : Component, INotifyPropertyChanged, IBindableComponent
{
public Component1()
{
InitializeComponent();
}
public Component1(IContainer container)
{
container.Add(this);
InitializeComponent();
}
private string teststring;
[Bindable(true)]
public string TestString
{
get
{
return teststring;
}
set
{
if (teststring != value)
{
teststring = value;
FirePropertyChanged("TestString");
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
void FirePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region IBindableComponent Members
private BindingContext bindingContext = null;
public BindingContext BindingContext
{
get
{
if (null == bindingContext)
{
bindingContext = new BindingContext();
}
return bindingContext;
}
set { bindingContext = value; }
}
private ControlBindingsCollection databindings;
public ControlBindingsCollection DataBindings
{
get
{
if (null == databindings)
{
databindings = new ControlBindingsCollection(this);
}
return databindings;
}
set { databindings = value; }
}
#endregion
}
print("code sample");
La solution
Essayez:
[ DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden ),
EditorBrowsable( EditorBrowsableState.Advanced ),
Browsable( false ) ]
public BindingContext BindingContext {
...
}
[ ParenthesizePropertyName( true ),
RefreshProperties( RefreshProperties.All ),
DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
Category( "Data" ) ]
public ControlBindingsCollection DataBindings {
...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow