Bindings look like the following:
XAML
<UserControl x:Class="myControl.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBlock Name="_innerCaption"
Text="{Binding MyCaption}"/>
</Grid>
</UserControl>
Now you have to set the DataContext of the XAML file to the class you would like to bind to:
namespace myControl
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
MyClassToBindTo bindingClass;
public UserControl1()
{
InitializeComponent();
bindingClass = new MyClassToBindTo();
DataContext = bindingClass;
}
}
}
And The Binding Class may look like this:
public class MyClassToBindTo : NotifyPropertyChangedBase
{
private string myCaptionString = "hello";
public string MyCaption
{
get { return myCaptionString }
set { myCaptionString = value; OnPropertyChanged("MyCaptionString"); }
}
// Also implement the INotifyPropertyChanged interface here
}
Hope this helps you!
EDIT:
I usually implement the INotifyPropertyChanged stuff in a base class "NotifyPropertyChangedBase:
public abstract class PropertyChangedBase : INotifyPropertyChanged
{
#region Member Variables
private static HashSet<string> alreadyCoveredProperties = new HashSet<string>();
#endregion
#region INotifyPropertyChanged Members
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Calls registered handlers when a property is changed.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
/// <summary>
/// Removes all property changed subscription from the event handler.
/// </summary>
protected void RemovePropertyChangedSubscription()
{
if (this.PropertyChanged != null)
{
this.PropertyChanged -= new PropertyChangedEventHandler(PropertyChanged);
this.PropertyChanged = null;
}
}
/// <summary>
/// Determines whether the specified property name refers to a property defined for this object.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns>
/// Returns <c>true</c> if the provided name refers to a defined property; otherwise, <c>false</c>.
/// </returns>
/// <remarks>Uses reflection.</remarks>
protected bool IsDefinedPropertyName(string propertyName)
{
string propertyFullName = GetType().FullName + "." + propertyName;
if (alreadyCoveredProperties.Contains(propertyFullName))
{
return true;
}
else
{
PropertyDescriptorCollection col = TypeDescriptor.GetProperties(this);
PropertyDescriptor propertyDescriptor = col[propertyName];
// A property exists, if the propertyDescriptor is != null.
if (propertyDescriptor != null)
{
alreadyCoveredProperties.Add(propertyFullName);
return true;
}
else
{
return false;
}
}
}
#endregion
}