How to: Create a Binding in Code

This example shows how to create and set a Binding in code.

Example

The FrameworkElement class and the FrameworkContentElement class both expose a SetBinding method. If you are binding an element that inherits either of these classes, you can call the SetBinding method directly.

The following example creates a class named, MyData, which contains a property named MyDataProperty.

Public Class MyData
    Implements INotifyPropertyChanged

    ' Events 
    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    ' Methods 
    Public Sub New()
    End Sub 

    Public Sub New(ByVal dateTime As DateTime)
        Me.MyDataProperty = ("Last bound time was " & dateTime.ToLongTimeString)
    End Sub 

    Private Sub OnPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub 


    ' Properties 
    Public Property MyDataProperty() As String 
        Get 
            Return Me._myDataProperty
        End Get 
        Set(ByVal value As String)
            Me._myDataProperty = value
            Me.OnPropertyChanged("MyDataProperty")
        End Set 
    End Property 


    ' Fields 
    Private _myDataProperty As String 
End Class
public class MyData : INotifyPropertyChanged
{
    private string myDataProperty;

    public MyData() { }

    public MyData(DateTime dateTime)
    {
        myDataProperty = "Last bound time was " + dateTime.ToLongTimeString();
    }

    public String MyDataProperty
    {
        get { return myDataProperty; }
        set
        {
            myDataProperty = value;
            OnPropertyChanged("MyDataProperty");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
}

The following example shows how to create a binding object to set the source of the binding. The example uses SetBinding to bind the Text property of myText, which is a TextBlock control, to MyDataProperty.

Dim myDataObject As New MyData(DateTime.Now)
Dim myBinding As New Binding("MyDataProperty")
myBinding.Source = myDataObject
Me.myText.SetBinding(TextBlock.TextProperty, myBinding)
//make a new source
MyData myDataObject = new MyData(DateTime.Now);      
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
myText.SetBinding(TextBlock.TextProperty, myBinding);

For the complete code sample, see Creating a Binding in Code Sample.

Instead of calling SetBinding, you can use the SetBinding static method of the BindingOperations class. The following example, calls BindingOperations.SetBinding instead of FrameworkElement.SetBinding to bind myText to myDataProperty.

Dim myDataObject As New MyData(DateTime.Now)
Dim myBinding As New Binding("MyDataProperty")
myBinding.Source = myDataObject
BindingOperations.SetBinding(myText, TextBlock.TextProperty, myBinding)
//make a new source
MyData myDataObject = new MyData(DateTime.Now);
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
BindingOperations.SetBinding(myText, TextBlock.TextProperty, myBinding);

See Also

Concepts

Data Binding Overview

Other Resources

Data Binding Samples

Data Binding How-to Topics

Change History

Date

History

Reason

January 2010

Added the declaration of the MyData class and changed the example that uses BindingOperations.SetBinding to parallel the example that uses FrameworkElement.SetBinding.

Content bug fix.