vincolante
OneWay Supponiamo di avere classe Country
con proprietà statica Name
.
public class Country
{
public static string Name { get; set; }
}
e ora si vuole vincolante proprietà Name
-TextProperty
di TextBlock
.
Binding binding = new Binding();
binding.Source = Country.Name;
this.tbCountry.SetBinding(TextBlock.TextProperty, binding);
Aggiornamento: TwoWay vincolante
Country
classe assomiglia a questo:
public static class Country
{
private static string _name;
public static string Name
{
get { return _name; }
set
{
_name = value;
Console.WriteLine(value); /* test */
}
}
}
Ed ora vogliamo vincolante questa proprietà Name
-TextBox
, quindi:
Binding binding = new Binding();
binding.Source = typeof(Country);
binding.Path = new PropertyPath(typeof(Country).GetProperty("Name"));
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
this.tbCountry.SetBinding(TextBox.TextProperty, binding);
Se si desidera aggiornare obiettivo è necessario utilizzare BindingExpression
e la funzione UpdateTarget
:
Country.Name = "Poland";
BindingExpression be = BindingOperations.GetBindingExpression(this.tbCountry, TextBox.TextProperty);
be.UpdateTarget();