OneWay Bindung
Nehmen wir an, dass Sie Klasse haben Country
mit statische Eigenschaft Name
.
public class Country
{
public static string Name { get; set; }
}
Und jetzt Sie wollen Bindungseigenschaft Name
-TextProperty
von TextBlock
.
Binding binding = new Binding();
binding.Source = Country.Name;
this.tbCountry.SetBinding(TextBlock.TextProperty, binding);
Update: TwoWay Bindung
Country
Klasse sieht wie folgt aus:
public static class Country
{
private static string _name;
public static string Name
{
get { return _name; }
set
{
_name = value;
Console.WriteLine(value); /* test */
}
}
}
Und jetzt wollen Bindung wir diese Eigenschaft Name
-TextBox
, so:
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);
Wenn Sie Ziel aktualisieren möchten, müssen Sie BindingExpression
und Funktion UpdateTarget
:
Country.Name = "Poland";
BindingExpression be = BindingOperations.GetBindingExpression(this.tbCountry, TextBox.TextProperty);
be.UpdateTarget();