Bindings in WPF and C#

Technologies.BindingWPFAndCSharp History

Hide minor edits - Show changes to markup

Added line 26:
Added lines 141-145:
Changed line 109 from:

All the other Bindings in the above example occur to an object. The object is set using the DataContext property in the code behind file or when using static objects the DataContext can be set in the XAML directly.

to:

All the other Bindings in the above example occur to an object. The object is set using the DataContext property in the code behind file or when using static objects the DataContext can be set in the XAML directly. The updates occur when the PropertyChanged event is triggered.

Changed line 38 from:

Binding to another XAML element

to:

Binding in general

Added lines 102-107:

In the above example the first label binds to the slider's value directly. The ElementName selects the desired element, the Path indicates the elements property (:source lang=csharp tabwidth=2 :) <Label Content="{Binding ElementName=slider_range, Path=Value}"></Label>

Changed lines 109-113 from:

Specifying the data source

to:

All the other Bindings in the above example occur to an object. The object is set using the DataContext property in the code behind file or when using static objects the DataContext can be set in the XAML directly.

Specifying the data source in code behind

(:source lang=csharp tabwidth=2 :) this.maingrid.DataContext = this;

Changed lines 86-87 from:

A property then needs to call the OnPropertyChanged method with its own name as a parameter.

to:

The property then needs to call the OnPropertyChanged method with its own name as a parameter.

Changed lines 38-39 from:

Binding in XAML

Binding to another element

to:

Binding to another XAML element

In this example the slider's value binds to the property Range on the code behind file. (:source lang=csharp tabwidth=2 :) <Window x:Class="Binding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:con="clr-namespace:Binding" Title="MainWindow" Height="700" Width="520" KeyDown="Window_KeyDown"> <Grid Name="maingrid"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition Height="auto" /> <RowDefinition Height="auto" /> </Grid.RowDefinitions> <Slider Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="500" Name="slider_range" Value="{Binding Path=Range, Mode=TwoWay}" Minimum="-500" Maximum="500" > </Slider> <DockPanel Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Margin="200 0 0 0"> <Label>Range element binding:</Label> <Label Content="{Binding ElementName=slider_range, Path=Value}"></Label> </DockPanel> <DockPanel Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Margin="200 0 0 0"> <Label>Range model binding:</Label> <Label Content="{Binding Path=Range, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></Label> </DockPanel> </Grid> </Window>

The Binding source needs to implement the INotifyPropertyChanged interface and needs to have a public event as follows. (:source lang=csharp tabwidth=2 :) public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } A property then needs to call the OnPropertyChanged method with its own name as a parameter. (:source lang=csharp tabwidth=2 :) private int range = 0; public int Range { get { return this.range; } set { if (this.range != value) { this.range = value; this.OnPropertyChanged("Range"); } } }

Changed line 102 from:

Specifying the data source

to:

Specifying the data source

Added lines 37-41:

Binding in XAML

Binding to another element

Binding to an object property

Specifying the data source

Changed lines 43-46 from:

- OneWay (default): from source to destination. - TwoWay: From source to destination and from destination to source. - OneWayToSource: from destination to source (opposite of OneWay). Meaning the destination does not get updated when the source's property value changes. - OneTime: causes the source property to initialize the target property, but subsequent changes do not propagate.

to:
  • OneWay (default): from source to destination.
  • TwoWay: From source to destination and from destination to source.
  • OneWayToSource: from destination to source (opposite of OneWay). Meaning the destination does not get updated when the source's property value changes.
  • OneTime: causes the source property to initialize the target property, but subsequent changes do not propagate.
Changed lines 54-56 from:

- Explicit: Only occurs when the application calls UpdateSource() - LostFocus: Source is updated when the target loses focus, e.g. when the TextBox control loses focus - PropertyChanged: Binding occurs immediately, e.g. as you type into the TextBox

to:
  • Explicit: Only occurs when the application calls UpdateSource()
  • LostFocus: Source is updated when the target loses focus, e.g. when the TextBox control loses focus
  • PropertyChanged: Binding occurs immediately, e.g. as you type into the TextBox
Added line 46:

- OneTime: causes the source property to initialize the target property, but subsequent changes do not propagate.

Changed line 53 from:

- Explicit: When the application calls UpdateSource()

to:

- Explicit: Only occurs when the application calls UpdateSource()

Changed line 35 from:

To define when and how often a target reads the value from the source you can use the UpdateSourceTrigger property.

to:

To define when and how often a target reads the value from the source you can use the UpdateSourceTrigger property.

Added lines 46-54:
back to top

When do updates occur

Bindings that occur from source to destination happens immediately. The property UpdateSourceTrigger changes the behaviour when Bindings include a direction from destination to source. - Explicit: When the application calls UpdateSource() - LostFocus: Source is updated when the target loses focus, e.g. when the TextBox control loses focus - PropertyChanged: Binding occurs immediately, e.g. as you type into the TextBox

Changed lines 35-37 from:

To define when and how often a the target reads the value from the source BindingTargetObject

to:

To define when and how often a target reads the value from the source you can use the UpdateSourceTrigger property. You bind a dependency property of a BindingTarget (the dependency object) to a property of a BindingSource

Changed lines 42-45 from:
to:

The direction can either be - OneWay (default): from source to destination. - TwoWay: From source to destination and from destination to source. - OneWayToSource: from destination to source (opposite of OneWay). Meaning the destination does not get updated when the source's property value changes.

Changed lines 34-35 from:

With bindings you can bind properties from a source object to a destination object (WPF). The destination object is

to:

With bindings you can bind properties from a source (code or WPF) to a target (WPF). The target usually reads a value from the source whenever the source value changes. This read-only binding is called OneWay binding. There are also different directions to bind a value. To define when and how often a the target reads the value from the source BindingTargetObject

Changed line 54 from:
to:
Added lines 33-34:

With bindings you can bind properties from a source object to a destination object (WPF). The destination object is

Added line 26:
Added lines 43-48:
back to top

Further readings

Data Binding (WPF) @MSDN pay special attention to the How-To section

Changed line 40 from:
to:

Added lines 1-43:

(:title Bindings in WPF and C#:)

WARNING! AVERTISSEMENT! ¡CUIDADO!
This project page is under construction! Last update was December 2013

Contents

back to top

Basics

back to top

Direction of Bindings

back to top

#converters

Data conversion

back to top