Bindings in WPF and C#

Technologies.BindingWPFAndCSharp History

Show minor edits - Show changes to output

Added line 26:
* [[#code| Sample project]]
Added lines 141-145:
!!!!!![[#top | back to top]]

[[#code]]
!Sample project
'''Download: [[Attach:BindingExample.zip | Binding examples source code, ZIP ]]'''
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 lines 1-43:
(:title Bindings in WPF and C#:)

%define=box padding-left=1em padding-right=1em margin='3px 3px 0'%
%define=yellowbox box bgcolor=#fdfaea border='1px solid #ffad80'%
%define=redbox box bgcolor=#fff3f3 border='1px solid #ffc9c9'%
%define=bluebox box bgcolor=#f4fbff border='1px solid #a1cae6'%
%define=skybox box bgcolor=#f8fcff border='1px solid #aaaaaa'%
%define=greybox box bgcolor=#fbfbfb border='1px solid #aaaaaa'%
%define=greenbox box bgcolor=#e6f3e5 border='1px solid #8fd586'%
%define=whitebox box bgcolor=#ffffff border='1px solid #999999'%

[[#top]]
>>redbox<<

%lfloat% Attach:warning.gif
'+'''WARNING! AVERTISSEMENT! ¡CUIDADO!'''+' \\
'+'''This project page is under construction! Last update was December 2013'''+'

>><<

!Contents
>>whitebox<<
* [[#basics | Basics]]
* [[#direction | Direction of Bindings]]
* [[#converters | Data conversion]]
>><<

!!!!!![[#top | back to top]]

[[#basics]]
!Basics

!!!!!![[#top | back to top]]

[[#direction]]
!Direction of Bindings

!!!!!![[#top | back to top]]

[[#converters ]]
!Data conversion

!!!!!![[#top | back to top]]