A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/binding-declarations-overview below:

Binding declarations overview - WPF

Typically, developers declare the bindings directly in the XAML markup of the UI elements they want to bind data to. However, you can also declare bindings in code. This article describes how to declare bindings in both XAML and in code.

Prerequisites

Before reading this article, it's important that you're familiar with the concept and usage of markup extensions. For more information about markup extensions, see Markup Extensions and WPF XAML.

This article doesn't cover data binding concepts. For a discussion of data binding concepts, see Data binding overview.

Declare a binding in XAML

Binding is a markup extension. When you use the binding extension to declare a binding, the declaration consists of a series of clauses following the Binding keyword and separated by commas (,). The clauses in the binding declaration can be in any order and there are many possible combinations. The clauses are Name=Value pairs, where Name is the name of the Binding property and Value is the value you're setting for the property.

When creating binding declaration strings in markup, they must be attached to the specific dependency property of a target object. The following example shows how to bind the TextBox.Text property using the binding extension, specifying the Source and Path properties.

<TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=Name}"/>

The previous example uses a simple data object type of Person. The following snippet is the code for that object:

class Person
{
    public string Name { get; set; }
    public DateTime Birthdate { get; set; }
}
Public Class Person

    Public Property Name As String
    Public Property Birthdate As DateTime
    
End Class

You can specify most of the properties of the Binding class this way. For more information about the binding extension and for a list of Binding properties that cannot be set using the binding extension, see the Binding Markup Extension (.NET Framework) overview.

For an example on creating a binding in XAML, see How to create a data binding.

Object element syntax

Object element syntax is an alternative to creating the binding declaration. In most cases, there's no particular advantage to using either the markup extension or the object element syntax. However, when the markup extension doesn't support your scenario, such as when your property value is of a non-string type for which no type conversion exists, you need to use the object element syntax.

The previous section demonstrated how to bind with a XAML extension. The following example demonstrates doing the same binding but uses object element syntax:

<TextBlock>
    <TextBlock.Text>
        <Binding Source="{StaticResource myDataSource}" Path="Name"/>
    </TextBlock.Text>
</TextBlock>

For more information about the different terms, see XAML Syntax In Detail (.NET Framework).

MultiBinding and PriorityBinding

MultiBinding and PriorityBinding don't support the XAML extension syntax. That's why you must use the object element syntax if you're declaring a MultiBinding or a PriorityBinding in XAML.

Create a binding in code

Another way to specify a binding is to set properties directly on a Binding object in code, and then assign the binding to a property. The following example shows how to create a Binding object in code.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Make a new data source object
    var personDetails = new Person()
    {
        Name = "John",
        Birthdate = DateTime.Parse("2001-02-03")
    };

    // New binding object using the path of 'Name' for whatever source object is used
    var nameBindingObject = new Binding("Name");

    // Configure the binding
    nameBindingObject.Mode = BindingMode.OneWay;
    nameBindingObject.Source = personDetails;
    nameBindingObject.Converter = NameConverter.Instance;
    nameBindingObject.ConverterCulture = new CultureInfo("en-US");

    // Set the binding to a target object. The TextBlock.Name property on the NameBlock UI element
    BindingOperations.SetBinding(NameBlock, TextBlock.TextProperty, nameBindingObject);
}
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)

    ' Make a new data source object
    Dim personDetails As New Person() With {
        .Name = "John",
        .Birthdate = Date.Parse("2001-02-03")
    }

    ' New binding object using the path of 'Name' for whatever source object is used
    Dim nameBindingObject As New Binding("Name")

    ' Configure the binding
    nameBindingObject.Mode = BindingMode.OneWay
    nameBindingObject.Source = personDetails
    nameBindingObject.Converter = NameConverter.Instance
    nameBindingObject.ConverterCulture = New CultureInfo("en-US")

    ' Set the binding to a target object. The TextBlock.Name property on the NameBlock UI element
    BindingOperations.SetBinding(NameBlock, TextBlock.TextProperty, nameBindingObject)

End Sub

The previous code set the following on the binding:

When the object you're binding is a FrameworkElement or a FrameworkContentElement, you can call the SetBinding method on your object directly instead of using BindingOperations.SetBinding. For an example, see How to: Create a Binding in Code.

The previous example uses a simple data object type of Person. The following is the code for that object:

class Person
{
    public string Name { get; set; }
    public DateTime Birthdate { get; set; }
}
Public Class Person

    Public Property Name As String
    Public Property Birthdate As DateTime
    
End Class
Binding path syntax

Use the Path property to specify the source value you want to bind to:

Escaping mechanism Binding direction

Use the Binding.Mode property to specify the direction of the binding. The following modes are the available options for binding updates:

For more information, see the BindingMode enumeration.

The following example shows how to set the Mode property:

<TextBlock Name="IncomeText" Text="{Binding Path=TotalIncome, Mode=OneTime}" />

To detect source changes (applicable to OneWay and TwoWay bindings), the source must implement a suitable property change notification mechanism such as INotifyPropertyChanged. For more information, see Providing change notifications.

For TwoWay or OneWayToSource bindings, you can control the timing of the source updates by setting the UpdateSourceTrigger property. For more information, see UpdateSourceTrigger.

Default behaviors

The default behavior is as follows if not specified in the declaration:

See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4