Last active
July 5, 2016 09:36
-
-
Save sakapon/81bc144d6453c148756f45817015e911 to your computer and use it in GitHub Desktop.
BindingSample / BindingConsole / ExpandoObject
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.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