Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winforms DataBind to Control's Visible Property

Are there any known issues when databinding to a control's visible property?

The control is always NOT visible regardless of what my property is.

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

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

I am trying to set a Panel's visible property.

like image 886
B Z Avatar asked Apr 08 '09 16:04

B Z


3 Answers

I've found that life is better if you assume that binding to a control's Visible property is broken, despite the fact that it sometimes works. See http://support.microsoft.com/kb/327305, which says as much (and while the KB article applies to .NET 1.0 and 1.1, it 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 centralized place to add a work-around. Instead of actually creating a binding on Visible it does two things:

  1. It subscribes to the data source's INotifyPropertyChanged.PropertyChanged event and sets the Visible value as appropriate when the event is raised.
  2. It sets the initial value of Visible according to the current data source value.

This required a little reflection code, but wasn't too bad. It is critical that you don't bind the Visible property and do the work-around or it won't work.

like image 181
Eric Smith Avatar answered Nov 08 '22 19:11

Eric Smith


Workaround: Set the Visible property on the BindingComplete event.

I had same issue setting a label's Visible property - always stays false, even though setting the Enabled property works fine.

like image 41
David Russell Avatar answered Nov 08 '22 19:11

David Russell


I just hit this issue in .NET 4.7.1 and Visual Studio 2017. To fix it, I changed the Visible property on my control to be initially set to True, as I had it as False previously.

like image 5
AndrewL Avatar answered Nov 08 '22 19:11

AndrewL