Skip to content

Instantly share code, notes, and snippets.

@cwensley
Last active August 29, 2015 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwensley/f6396518fb56c95f3a75 to your computer and use it in GitHub Desktop.
Save cwensley/f6396518fb56c95f3a75 to your computer and use it in GitHub Desktop.
Shows how to bind a control's BackgroundProperty from a bool
using System;
using System.ComponentModel;
using Eto;
using Eto.Forms;
using Eto.Drawing;
namespace BindingBackgroundColorFromBool
{
public class Model : INotifyPropertyChanged
{
string text;
bool isValid = true;
public string Text
{
get { return text; }
set
{
text = value;
OnPropertyChanged("Text");
IsValid = text == null || text.Length <= 4;
}
}
public bool IsValid
{
get { return isValid; }
set
{
if (value != isValid)
{
isValid = value;
OnPropertyChanged("IsValid");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class TestForm : Form
{
public TestForm()
{
Menu = new MenuBar();
var textBox = new TextBox();
var defaultColor = textBox.BackgroundColor;
textBox.TextBinding.BindDataContext<Model>(r => r.Text);
textBox.BindDataContext(c => c.BackgroundColor, Binding.Property((Model m) => m.IsValid).Convert(val => !val ? Colors.Red : defaultColor));
Content = new TableLayout
{
Padding = new Padding(10),
Spacing = new Size(5, 5),
Rows =
{
new TableRow(new Label { Text = "Text:" }, textBox),
}
};
// set the data context
DataContext = new Model();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment