0

In Flex (AS3) you can do neat things where you bind a property to an expression/variable/object. e.g. button.enabled = {!obj.name.empty} It makes for very neat GUI validation amongst other things.

I don't see any such facility in Visual Studio GUI designer but wondered if this sort of functionality exists in .Net/C# and if so, to what extent?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • 1
    In what context are you talking about? XAML (WPF and Silverlight) developers are probably used to two way databinding... – mason Oct 13 '15 at 16:55
  • Probably a standard C# Winforms application – Mr. Boy Oct 13 '15 at 16:59
  • Yes, it involves a [`PropertyManager`](https://msdn.microsoft.com/en-us/library/system.windows.forms.propertymanager(v=vs.110).aspx) and implementing the [`INotifyPropertyChanged`](https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx) interface. (It cannot be done to a *variable* which doesn't make sense in c# anyway) – Erik Philips Oct 13 '15 at 17:03

1 Answers1

5

I don't see any such facility in Visual Studio GUI designer

In fact there is. Select the form/control in the designer, then go to Properties window and expand the (DataBindings) category. You'll see a couple properties there, and more if you click the (Advanced) item.

to what extent?

It could only bind to a property (no expressions are supported). To make it work, the object providing the property must support property change notification. The standard way is implementing INotifyPropertyChanged interface, but there are also other mechanisms - IBindingListimplementation providing ListChanged event, object providing event named PropertyNameChanged for a PropertyName etc.

As I mentioned, standartly you can bind only to properties. However, at runtime with some helpers you can really bind to a method or expression. I've already provided examples of doing that in the following threads Button enable and disable on text changed event, Exchange UserControls on a Form with data-binding, Custom WinForms data binding with converter not working on nullable type (double?), .Net WinForms design to sync Data and Controls for a single item data binding, and my own question Cross tabular data binding in WPF for more complex scenarios.

Contrary to what some WPF-ers say, there are no WF limits when binding to a custom objects/collections. The only limits are when binding to other control properties because control property notification pattern is not strictly followed like in WPF where it is by design.

Community
  • 1
  • 1
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343