Files
docs-desktop/dotnet-desktop-guide/framework/wpf/data/binding-declarations-overview.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

11 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
Binding Declarations Overview Learn how to declare a binding in XAML for your application development in Windows Presentation Foundation (WPF). 03/30/2017
csharp
vb
markup extensions [WPF]
data binding [WPF], declarations
object element syntax [WPF]
binding data [WPF], declarations
syntax [WPF], object elements
binding declarations [WPF]
b97fd626-4c0d-4761-872a-2bca5820da2c

Binding Declarations Overview

This topic discusses the different ways you can declare a binding.

Prerequisites

Before reading this topic, it is important that you are familiar with the concept and usage of markup extensions. For more information about markup extensions, see Markup Extensions and WPF XAML.

This topic does not cover data binding concepts. For a discussion of data binding concepts, see Data Binding Overview.

Declaring a Binding in XAML

This section discusses how to declare a binding in XAML.

Markup Extension Usage

xref:System.Windows.Data.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 xref:System.Windows.Data.Binding property and Value is the value you are 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 xref:System.Windows.Controls.TextBox.Text%2A?displayProperty=nameWithType property using the binding extension, specifying the xref:System.Windows.Data.Binding.Source%2A and xref:System.Windows.Data.Binding.Path%2A properties.

[!code-xamlSimpleBinding]

You can specify most of the properties of the xref:System.Windows.Data.Binding class this way. For more information about the binding extension as well as for a list of xref:System.Windows.Data.Binding properties that cannot be set using the binding extension, see the Binding Markup Extension overview.

Object Element Syntax

Object element syntax is an alternative to creating the binding declaration. In most cases, there is no particular advantage to using either the markup extension or the object element syntax. However, in cases which the markup extension does not 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 following is an example of both the object element syntax and the markup extension usage:

[!code-xamlBindConversionMarkup#1]

The example binds the xref:System.Windows.Controls.TextBlock.Foreground%2A property by declaring a binding using the extension syntax. The binding declaration for the xref:System.Windows.Controls.TextBlock.Text%2A property uses the object element syntax.

For more information about the different terms, see XAML Syntax In Detail.

MultiBinding and PriorityBinding

xref:System.Windows.Data.MultiBinding and xref:System.Windows.Data.PriorityBinding do not support the XAML extension syntax. Therefore, you must use the object element syntax if you are declaring a xref:System.Windows.Data.MultiBinding or a xref:System.Windows.Data.PriorityBinding in XAML.

Creating a Binding in Code

Another way to specify a binding is to set properties directly on a xref:System.Windows.Data.Binding object in code. The following example shows how to create a xref:System.Windows.Data.Binding object and specify the properties in code. In this example, TheConverter is an object that implements the xref:System.Windows.Data.IValueConverter interface.

[!code-csharpBindConversion#1] [!code-vbBindConversion#1]

If the object you are binding is a xref:System.Windows.FrameworkElement or a xref:System.Windows.FrameworkContentElement you can call the SetBinding method on your object directly instead of using xref:System.Windows.Data.BindingOperations.SetBinding%2A?displayProperty=nameWithType. For an example, see Create a Binding in Code.

Binding Path Syntax

Use the xref:System.Windows.Data.Binding.Path%2A property to specify the source value you want to bind to:

  • In the simplest case, the xref:System.Windows.Data.Binding.Path%2A property value is the name of the property of the source object to use for the binding, such as Path=PropertyName.

  • Subproperties of a property can be specified by a similar syntax as in C#. For instance, the clause Path=ShoppingCart.Order sets the binding to the subproperty Order of the object or property ShoppingCart.

  • To bind to an attached property, place parentheses around the attached property. For example, to bind to the attached property xref:System.Windows.Controls.DockPanel.Dock%2A?displayProperty=nameWithType, the syntax is Path=(DockPanel.Dock).

  • Indexers of a property can be specified within square brackets following the property name where the indexer is applied. For instance, the clause Path=ShoppingCart[0] sets the binding to the index that corresponds to how your property's internal indexing handles the literal string "0". Nested indexers are also supported.

  • Indexers and subproperties can be mixed in a Path clause; for example, Path=ShoppingCart.ShippingInfo[MailingAddress,Street].

  • Inside indexers you can have multiple indexer parameters separated by commas (,). The type of each parameter can be specified with parentheses. For example, you can have Path="[(sys:Int32)42,(sys:Int32)24]", where sys is mapped to the System namespace.

  • When the source is a collection view, the current item can be specified with a slash (/). For example, the clause Path=/ sets the binding to the current item in the view. When the source is a collection, this syntax specifies the current item of the default collection view.

  • Property names and slashes can be combined to traverse properties that are collections. For example, Path=/Offices/ManagerName specifies the current item of the source collection, which contains an Offices property that is also a collection. Its current item is an object that contains a ManagerName property.

  • Optionally, a period (.) path can be used to bind to the current source. For example, Text="{Binding}" is equivalent to Text="{Binding Path=.}".

Escaping Mechanism

  • Inside indexers ([ ]), the caret character (^) escapes the next character.

  • If you set xref:System.Windows.Data.Binding.Path%2A in XAML, you also need to escape (using XML entities) certain characters that are special to the XML language definition:

    • Use & to escape the character "&".

    • Use > to escape the end tag ">".

  • Additionally, if you describe the entire binding in an attribute using the markup extension syntax, you need to escape (using backslash \) characters that are special to the [!INCLUDETLA2#tla_winclient] markup extension parser:

    • Backslash (\) is the escape character itself.

    • The equal sign (=) separates property name from property value.

    • Comma (,) separates properties.

    • The right curly brace (}) is the end of a markup extension.

Default Behaviors

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

See also