Vorgehensweise: Erstellen einer Bindung in CodeHow to: Create a Binding in Code

Dieses Beispiel veranschaulicht das Erstellen und Festlegen einer Binding im Code.This example shows how to create and set a Binding in code.

BeispielExample

Die FrameworkElement Klasse und die FrameworkContentElement Klasse, die beide verfügbar machen, eine SetBinding Methode.The FrameworkElement class and the FrameworkContentElement class both expose a SetBinding method. Wenn Sie ein Element, die eine dieser Klassen erbt binden, können Sie rufen die SetBinding -Methode direkt.If you are binding an element that inherits either of these classes, you can call the SetBinding method directly.

Das folgende Beispiel erstellt eine Klasse, die mit dem Namen MyData, enthält eine Eigenschaft namens MyDataProperty.The following example creates a class named, MyData, which contains a property named MyDataProperty.

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));
        }
    }
}
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

Das folgende Beispiel zeigt, wie Sie erstellen ein Binding-Objekt, um die Quelle der Bindung festgelegt wird.The following example shows how to create a binding object to set the source of the binding. Im Beispiel wird SetBinding zum Binden der Text Eigenschaft myText, d.h. eine TextBlock Steuerelements, auf MyDataProperty.The example uses SetBinding to bind the Text property of myText, which is a TextBlock control, to MyDataProperty.

// Make a new source.
MyData myDataObject = new MyData(DateTime.Now);      
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
// Bind the new data source to the myText TextBlock control's Text dependency property.
myText.SetBinding(TextBlock.TextProperty, myBinding);
' Make a new source.
Dim data1 As New MyData(DateTime.Now)
Dim binding1 As New Binding("MyDataProperty")
binding1.Source = data1
' Bind the new data source to the myText TextBlock control's Text dependency property.
Me.myText.SetBinding(TextBlock.TextProperty, binding1)

Das vollständige Codebeispiel finden Sie unter nur ein Beispiel für die Bindung.For the complete code sample, see Code-only Binding Sample.

Statt SetBinding, können Sie die SetBinding statische Methode der BindingOperations Klasse.Instead of calling SetBinding, you can use the SetBinding static method of the BindingOperations class. Das folgende Beispiel auszuführen, ruft BindingOperations.SetBinding anstelle von FrameworkElement.SetBinding binden myText zu myDataProperty.The following example, calls BindingOperations.SetBinding instead of FrameworkElement.SetBinding to bind myText to myDataProperty.

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

Siehe auchSee also