Skip to content

Latest commit

 

History

History
76 lines (47 loc) · 4.96 KB

binding.md

File metadata and controls

76 lines (47 loc) · 4.96 KB
-api-id -api-type
T:Windows.UI.Xaml.Data.Binding
winrt class

Windows.UI.Xaml.Data.Binding

-description

Defines a binding that connects the properties of binding targets and data sources.

-xaml-syntax

<Binding .../>
- or -
<dependencyobject dependencyproperty="{Binding bindingArgs}" />

-remarks

The {Binding} markup extension enables you to specify a Binding value as a single attribute string in XAML, including setting Binding properties such as Path and Source. For more info about data binding concepts, see Data binding in depth.

The Binding class might be considered the code-behind exposure of the {Binding} markup extension. If a binding is already applied to a target (which happens when the XAML is loaded), you can't set the read-write properties of a Binding object to change how a binding behaves at run-time. Any XAML-defined binding should be considered immutable. But you can create a new Binding object, set its properties, and establish a new binding on a specific UI element target using FrameworkElement.SetBinding. For more info, see Creating bindings in code.

A Binding object connects a dependency property of a FrameworkElement directly to a data object so that updates to the data object are automatically propagated to the property that uses data binding. The Binding class defines the properties of a binding. Each binding must have a target element, target property, and data source, although some values are provided by default if you don't specify them.

To bind to a property or a sub-property on a data object, set the Path property of the Binding object. For more info on how to set Path in code or in XAML, see Property-path syntax or {Binding} markup extension.

You can apply an instance of a Binding class to multiple targets. However, you cannot modify the property values of a Binding object after you attach it to a target element.

Note

Calling the FrameworkElement.SetBinding method and passing in a new Binding object won't necessarily remove an existing binding. Instead, you should use the DependencyObject.ClearValue method.

For more info on XAML attribute usage for properties that can take a Binding, or that can otherwise be set to a data-bound value, see {Binding} markup extension.

The property that is the target of a data binding must be a dependency property. For more info, see Dependency properties overview.

-examples

The following code example demonstrates how to create a binding in XAML. For the complete code listing, see the XAML data binding sample.

<StackPanel Margin="5">

  <TextBlock Text="Name:" Style="{StaticResource DescriptionTextStyle}" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <TextBox Text="{Binding Path=Name, Mode=TwoWay}" 
    Width="350" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <TextBlock Text="Organization:" Style="{StaticResource DescriptionTextStyle}" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

  <!-- You can omit the 'Path=' portion of the binding expression. --> 
  <TextBox Text="{Binding Organization, Mode=TwoWay}" Width="350" 
    Margin="5" HorizontalAlignment="Left" VerticalAlignment="Top"/>

</StackPanel>

The following example code demonstrates how to create a binding in code.

[!code-csharpBindingObject]

[!code-cppBindingObject]

[!code-vbBindingObject]

-see-also

XAML data binding sample, Data binding in depth, Dependency properties overview, Property-path syntax, BindingOperations.SetBinding