views:

679

answers:

3

WinForms, .NetFramework 3.5

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.

Using a BindingSource.

Thx in advance.

A: 

Things to check:

  • Be sure you've instantiated the class that has the IsRibbonCategory property
  • Did you set the datasource of property of the binding source to the instance of the class
  • The datasource update mode should be on "on validation"
  • Make sure you didn't set the visible property manually to false on the control

Hope that helps. Can you post more code?

matt eisenberg
I have checked all of those, but no dice. One question, have you tried databinding to visible property and it worked?
B Z
Yes. VS 2008/Winforms. What event do you use to set the datasource of the bindingsource control?
matt eisenberg
The form's constructor.
B Z
Have you tried calling bindingsource.CurrencyManager.Refresh in the form's load event?
matt eisenberg
no, I will give that a shot.
B Z
No, that didn't work. I will try to put a small prototype together
B Z
+2  A: 

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.

Dave
+1  A: 

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.

Eric Smith