A workaround would be to use the component to bind to the visibility control attribute instead of directly bind to the control's visibility property. See below code:
using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication2 { public class ControlVisibilityBinding : Component { private static readonly object EventControlChanged = new object(); private static readonly object EventVisibleChanged = new object(); private System.Windows.Forms.Control _control; private bool _visible = true; public event EventHandler VisibleChanged { add { Events.AddHandler(EventVisibleChanged, value); } remove { Events.RemoveHandler(EventVisibleChanged, value); } } public event EventHandler ControlChanged { add { Events.AddHandler(EventControlChanged, value); } remove { Events.RemoveHandler(EventControlChanged, value); } } public ControlVisibilityBinding() { } public ControlVisibilityBinding(IContainer container) { container.Add(this); } [DefaultValue(null)] public System.Windows.Forms.Control Control { get { return _control; } set { if(_control == value) { return; } WireControl(_control, false); _control = value; if(_control != null) { _control.Visible = _visible; } WireControl(_control, true); OnControlChanged(EventArgs.Empty); OnVisibleChanged(EventArgs.Empty); } } [DefaultValue(true)] public bool Visible { get { return _visible; } set { if(_visible != value) { _visible = value; } if(Control != null) { Control.Visible = _visible; } OnVisibleChanged(EventArgs.Empty); } } private void WireControl(Control control, bool subscribe) { if(control == null) { return; } if(subscribe) { control.VisibleChanged += Control_VisibleChanged; } else { control.VisibleChanged -= Control_VisibleChanged; } } private void Control_VisibleChanged(object sender, EventArgs e) { OnVisibleChanged(EventArgs.Empty); } protected virtual void OnVisibleChanged(EventArgs e) { EventHandler subscribers = (EventHandler)Events[EventVisibleChanged]; if(subscribers != null) { subscribers(this, e); } } protected virtual void OnControlChanged(EventArgs e) { EventHandler subscribers = (EventHandler)Events[EventControlChanged]; if(subscribers != null) { subscribers(this, e); } } } static class Program { [STAThread] static void Main() { using(Form form = new Form()) using(FlowLayoutPanel groupBoxLayoutPanel = new FlowLayoutPanel()) using(RadioButton visibleButton = new RadioButton()) using(RadioButton hiddenButton = new RadioButton()) using(GroupBox groupBox = new GroupBox()) using(Label text = new Label()) using(ControlVisibilityBinding visibilityBinding = new ControlVisibilityBinding()) using(TextBox inputTextBox = new TextBox()) { groupBoxLayoutPanel.Dock = DockStyle.Fill; groupBoxLayoutPanel.FlowDirection = FlowDirection.LeftToRight; groupBoxLayoutPanel.AutoSize = true; groupBoxLayoutPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink; visibleButton.Text = "Show Label"; visibleButton.AutoSize = true; hiddenButton.Text = "Hide Label"; hiddenButton.AutoSize = true; groupBoxLayoutPanel.Controls.Add(visibleButton); groupBoxLayoutPanel.Controls.Add(hiddenButton); inputTextBox.Text = "Enter Label Text Here"; inputTextBox.Dock = DockStyle.Top; groupBox.AutoSize = true; groupBox.AutoSizeMode = AutoSizeMode.GrowAndShrink; groupBox.Controls.Add(groupBoxLayoutPanel); groupBox.Dock = DockStyle.Fill; text.AutoSize = true; text.ForeColor = Color.Red; text.Dock = DockStyle.Bottom; text.BorderStyle = BorderStyle.FixedSingle; text.Font = new Font(text.Font.FontFamily, text.Font.Size * 1.25f, FontStyle.Bold | FontStyle.Italic); text.DataBindings.Add("Text", inputTextBox, "Text", true, DataSourceUpdateMode.Never); visibilityBinding.Control = text; visibleButton.DataBindings.Add("Checked", visibilityBinding, "Visible", true, DataSourceUpdateMode.OnPropertyChanged); Binding binding = hiddenButton.DataBindings.Add("Checked", visibilityBinding, "Visible", true, DataSourceUpdateMode.OnPropertyChanged); ConvertEventHandler invertConverter = (sender, e) => e.Value = !((bool)e.Value); binding.Format += invertConverter; binding.Parse += invertConverter; form.Controls.Add(inputTextBox); form.Controls.Add(text); form.Controls.Add(groupBox); Application.Run(form); } } }
}
Hasani blackwell
source share