Winforms DataBind for Visible Property Management

WinForms, .NetFramework 3.5

Are there any known issues when binding data to a visible property of a control?

The control is always NOT displayed no matter what I have.

Public ReadOnly Property IsRibbonCategory() As Boolean Get Return True End Get End Property 

I tried the text control property and other properties, and they seem to work correctly.

I am trying to set the Panel property visible.

Using a binding source.

thanks in advance.

+6
data-binding winforms
source share
6 answers

Workaround: Set the Visible property in the BindingComplete event.

I had the same problem with setting the label Visible property - always remains false, even if the Enabled property setting works fine.

+3
source share

I found that life is better if you assume that the binding to the Visible property control is broken, even though it sometimes works. See http://support.microsoft.com/kb/327305 which says the same (and although KB article refers to .NET 1.0 and 1.1, this still seems to be a problem in at least 2.0).

I created a utility class for creating bindings, which, among other things, gave me a central place to add a workflow. Instead of actually creating a binding on Visible, it performs two functions:

  • It attaches to the INotifyPropertyChanged.PropertyChanged event of the data source and sets the value to Visible, when necessary, when the event occurs.
  • It sets the initial Visible value to the current value of the data source.

This required a small reflection code, but it was not so bad. It is very important that you do not bind the Visible property to work, or that does not work.

+8
source share

What you need to check:

  • Make sure you instantiate the class with the IsRibbonCategory property
  • You set the binding source property data source to an instance of the class
  • The data source update mode must be enabled "during validation"
  • Make sure you do not set the visible property manually to false in the control

Hope this helps. Can you post more code?

+1
source share

I just ran into this problem in .NET 4.7.1 and Visual Studio 2017. To fix this, I changed the Visible property on my control so that it was initially set to True , as before, to False .

+1
source share

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); } } } 

}

0
source share

Here is my turn, it may be stupid, but it has worked many times.

I put one Panel control on the form, I do this to fill out the form, and I put everything in this panel. All the controls with which I bind the Visible property see a change in their visibility according to the objects in my DataGridView.

Form structure

0
source share

All Articles