Binding to a TextBox programmatically
We start with the text box in the XAML:
<TextBox x:Name="tbName" Text="{Binding Name, UpdateSourceTrigger = PropertyChanged}" />
There are 2 important items here.
The 1st is the Binding to the dependancy property declared in the code behind: 'Binding Name'
The 2nd is the UpdateSourceTrigger. Setting it to 'PropertyChanged' triggers the binding whenever the content in the box has been chagned.
Without this, the binding is only updated on the LostFocus event. You would have to click on another form element or somehow change the focus for the changes in the text box to be noticed.
A 3rd property (not shown here) is the Binding Mode. The default for a TextBox is 'TwoWay' so it was not necessary to add it. The other choices are 'OneWay', etc...
In the defination of the XAML file, the DataContext must be defined:
DataContext="{Binding RelativeSource = {RelativeSource Self}}"
Forget this and it will not know where to find the 'Name' property defined in the text box binding.
Define the DependencyProperty in the code behind page of the user control.
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof (string),
typeof (MyUserControl), new UIPropertyMetadata(string.Empty));
When you add 'MyUserControl' to a window or control programmatically, the binding must be spelled out like this:
MyUserControl myUserControl = new MyUserControl(); // Create a new instance of the user control Binding nameBinding= new Binding(); // Create a new binding nameBinding.Source = personObject; // The source of the the object itself, not the specific property of the object nameBinding.Path = new PropertyPath(personObject.Name); // The path is specifies what property in the object is bound nameBinding.Mode = BindingMode.TwoWay; // Again the mode is set to TwoWay nameBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; // Again the trigger is set to PropertyChange myUserControl.SetBinding(MyUserControl.NameProperty, nameBinding); // then the binding is set to the dependency property
You can see that several properties (Trigger and Mode) are set both here and in the XAML
Binding and Dependency Properties
I found a great reference cheet for Binding over at ndb-tech. Thanks guys!
The quick and dirty way to bind a property to a XAML item is to make that property a Dependency property. Then you can bind it to another property in the XAML.
Here is an example o the XAML:
<Canvas >
<TextBox Name="tb1" Canvas.Top="{Binding ElementName=thisPopup, Path=CanvasTop}" />
</Canvas>
I found it odd that you have to name your UserControl for it to work. I am sure my syntax is a bit off and it is not necessary.
<UserControl x:Class="Sandbox.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="myControl">
The dependency property:
public static DependencyProperty CanvasTopProperty = DependencyProperty.Register("CanvasTop", typeof(double), typeof(PopupControl));
public double CanvasTop
{
get { return (double)GetValue(CanvasTopProperty); }
set { SetValue(CanvasTopProperty, value); }
}