Oneway बाध्यकारी
मान लें कि आप स्थिर संपत्ति Name
साथ वर्ग Country
करते हैं।
public class Country
{
public static string Name { get; set; }
}
और अब आप TextBlock
की TextProperty
को संपत्ति Name
बाध्यकारी चाहते हैं।
Binding binding = new Binding();
binding.Source = Country.Name;
this.tbCountry.SetBinding(TextBlock.TextProperty, binding);
अद्यतन: TwoWay बाध्यकारी
Country
वर्ग इस तरह दिखता है:
public static class Country
{
private static string _name;
public static string Name
{
get { return _name; }
set
{
_name = value;
Console.WriteLine(value); /* test */
}
}
}
और अब हम TextBox
को यह संपत्ति Name
बाध्यकारी चाहते हैं, इसलिए:
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);
आप लक्ष्य को अद्यतन चाहते हैं तो आप BindingExpression
और समारोह UpdateTarget
का उपयोग करना चाहिए:
Country.Name = "Poland";
BindingExpression be = BindingOperations.GetBindingExpression(this.tbCountry, TextBox.TextProperty);
be.UpdateTarget();