如何:在代码中创建绑定

此示例显示如何在代码中创建和设置 Binding

示例

FrameworkElement 类和 FrameworkContentElement 类都公开了一个 SetBinding 方法。 如果要绑定继承这两个类中的任何一个的元素,则可以直接调用 SetBinding 方法。

下面的示例创建一个名为 MyData 的类,它包含一个名为 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

下面的示例演示如何创建绑定对象来设置绑定的源。 该示例使用 SetBindingmyText(一个 TextBlock 控件)的 Text 属性绑定到 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)

有关完整的代码示例,请参阅仅限代码的绑定示例

可以使用 SetBinding 类的 SetBinding 静态方法,而不是调用 BindingOperations。 下面的示例调用 BindingOperations.SetBinding 而不是 FrameworkElement.SetBinding 来将 myText 绑定到 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)

另请参阅