Beginner databinding and MVVM question

305 views
Skip to first unread message

menn...@gmail.com

unread,
Feb 5, 2015, 6:37:54 AM2/5/15
to eto-...@googlegroups.com
Hi,

I recently started with Eto, coming from WPF. What I was hoping to achieve is the following: I have a view model that implements INotifyPropertyChanged with a boolean property IsValid and a String property Value. I can easily bind the string value to a TextBox Text using TextBinding.
I would also like to bind the BackgroundColor to the IsValid property to show validation errors by e.g. a red background of the TextBox.

In WPF this can be achieved with a Converter in the binding, that converts boolean values to color values. How would I go about setting up a view-viewmodel binding to support changing the background color of a TextBox?

Many thanks!

menno

curtis

unread,
Feb 5, 2015, 2:15:52 PM2/5/15
to eto-...@googlegroups.com
Hi Menno,

Yeah coming from WPF, Eto.Forms' binding is quite different.  However, it is still quite flexible and you can convert your bindings similarly.

Bindings have a Convert() method which can convert any binding to another type using delegates.  To create the binding, you use the Eto.Binding.Property or Delegate static methods instead of specifying the binding directly with Bind() or BindDataContext(), which then makes it easy to convert them.

E.g. the following two lines do the same thing:

textBox.BindDataContext(c => c.Text, (Model m) => m.Text);
textBox.BindDataContext(c => c.Text, Binding.Property((Model m) => m.Text));


Taking that to the BackgroundColor property, you can then convert it easily from bool to a Color:

textBox.BindDataContext(c => c.BackgroundColor, Binding.Property((Model m) => m.IsValid).Convert(val => !val ? Colors.Red : defaultColor));

I created a full sample that shows how you can do what you are trying to achieve.

Hope this helps!
Curtis.
Reply all
Reply to author
Forward
0 new messages