Last active
August 29, 2015 14:14
-
-
Save cwensley/f6396518fb56c95f3a75 to your computer and use it in GitHub Desktop.
Shows how to bind a control's BackgroundProperty from a bool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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