Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active July 17, 2016 09:28
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/2e28273c8a3706e43969730b9a3a90fe to your computer and use it in GitHub Desktop.
Save sakapon/2e28273c8a3706e43969730b9a3a90fe to your computer and use it in GitHub Desktop.
BindingSample / BindingConsole / POCO
using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
namespace BindingConsole
{
class Program
{
[STAThread]
static void Main(string[] args)
{
// Binding Source (Any object).
var person = new Person0 { Id = 123, Name = "Taro" };
// Binding Target (DependencyObject).
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.
// Notification does not work in usual property setting.
//person.Name = "Jiro";
var properties = TypeDescriptor.GetProperties(person);
properties[nameof(person.Name)].SetValue(person, "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