Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active July 5, 2016 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/81bc144d6453c148756f45817015e911 to your computer and use it in GitHub Desktop.
Save sakapon/81bc144d6453c148756f45817015e911 to your computer and use it in GitHub Desktop.
BindingSample / BindingConsole / ExpandoObject
using System;
using System.Dynamic;
using System.Windows.Controls;
using System.Windows.Data;
namespace BindingConsole
{
class Program
{
[STAThread]
static void Main(string[] args)
{
// Binding Source.
dynamic person = new ExpandoObject();
person.Id = 123;
person.Name = "Taro";
// Binding Target must be FrameworkElement.
var textBox = new TextBox { Text = "Default" };
Console.WriteLine(textBox.Text); // Default
// Binds target to source.
var binding = new Binding(nameof(person.Name)) { Source = person, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
textBox.SetBinding(TextBox.TextProperty, binding);
Console.WriteLine(textBox.Text); // Taro
// Changes source value.
person.Name = "Jiro";
Console.WriteLine(textBox.Text); // Jiro
// Changes target value.
textBox.Text = "Saburo";
Console.WriteLine(person.Name); // Saburo
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment