Jak utworzyć powiązanie w kodzie

W tym przykładzie pokazano, jak utworzyć i ustawić Binding element w kodzie.

Przykład

Klasa FrameworkElement i FrameworkContentElement klasa uwidaczniają metodę SetBinding . Jeśli wiążesz element, który dziedziczy jedną z tych klas, możesz wywołać metodę SetBinding bezpośrednio.

Poniższy przykład tworzy klasę o nazwie , MyDataktóra zawiera właściwość o nazwie 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

W poniższym przykładzie pokazano, jak utworzyć obiekt powiązania w celu ustawienia źródła powiązania. W przykładzie użyto SetBinding metody , aby powiązać Text właściwość myText, która jest kontrolką , z elementem TextBlockMyDataProperty.

// 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)

Aby zapoznać się z kompletnym przykładem kodu, zobacz Przykład powiązania tylko do kodu.

Zamiast wywoływać SetBindingmetodę SetBinding , można użyć metody statycznej BindingOperations klasy . W poniższym przykładzie wywołania BindingOperations.SetBinding zamiast wiązać myText się z FrameworkElement.SetBinding elementem 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)

Zobacz też