Pregunta

Tengo un componente personalizado donde he implementadoINotifyPropertyChanged y IBindableComponent.

Sin embargo, cuando intento databind una propiedad, el diseñador agrega esta línea:

this.component11.TestString =
global::WindowsFormsApplication2.Properties.Settings.Default.Setting;

en lugar de crear un enlace como lo hace con un TextBox:

this.textBox2.DataBindings.Add(new System.Windows.Forms.Binding(
   "Text",
   global::WindowsFormsApplication2.Properties.Settings.Default,
   "Setting2",
   true,
   System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

Pensé que el diseñador simplemente miraría para ver siIBindableComponent se implementa y, si es así, genere la codificación de enlace en lugar del código de asignación.

¿Alguna idea de por qué esto funciona con un cuadro de texto y no con mi componente personalizado?

Aquí está mi componente personalizado:

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");
¿Fue útil?

Solución

Intentar:

[ DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden ),
  EditorBrowsable( EditorBrowsableState.Advanced ),
  Browsable( false ) ]
public BindingContext BindingContext {
    ...
}

[ ParenthesizePropertyName( true ),
  RefreshProperties( RefreshProperties.All ),
  DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
  Category( "Data" ) ]
public ControlBindingsCollection DataBindings {
   ...
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top