DataBinding allows the flow of data between UI and business model. Any modification done on data in your business model after binding is done, will automatically reflect in the UI, and vice versa. This post is based on an article over at C# Corner called DataBinding In WPF . There are four types.
OneWay
TwoWay
OneWayToSource
OneTime
One Way XAML
1
<
Window
x:Class
=
"DataBindingCSharpCorner1Way.MainWindow"
6
xmlns:local
=
"clr-namespace:DataBindingCSharpCorner1Way"
8
Title
=
"DataBindingCSharpCorner1Way"
Height
=
"150"
Width
=
"370"
>
11
<
RowDefinition
Height
=
"Auto"
/>
12
<
RowDefinition
Height
=
"Auto"
/>
13
<
RowDefinition
Height
=
"Auto"
/>
14
<
RowDefinition
Height
=
"*"
/>
15
</
Grid.RowDefinitions
>
16
<
Grid.ColumnDefinitions
>
17
<
ColumnDefinition
Width
=
"Auto"
/>
18
<
ColumnDefinition
Width
=
"200"
/>
19
</
Grid.ColumnDefinitions
>
20
<
Label
Name
=
"nameLabel"
Margin
=
"2"
>_Name:</
Label
>
21
<
TextBox
Name
=
"nameText"
Grid.Column
=
"1"
Margin
=
"2"
Text
=
"{Binding FirstName, Mode=OneWay}"
/>
22
<
Label
Name
=
"titleLabel"
Margin
=
"2"
Grid.Row
=
"1"
>_Title:</
Label
>
23
<
TextBox
Name
=
"titleText"
Grid.Column
=
"1"
Grid.Row
=
"1"
Margin
=
"2"
Text
=
"{Binding Title, Mode=OneWay}"
/>
24
<
StackPanel
Grid.Row
=
"2"
Grid.ColumnSpan
=
"2"
>
25
<
Button
Content
=
"_Show..."
Click
=
"Button_Click"
/>
27
<
TextBlock
Grid.Row
=
"3"
Grid.ColumnSpan
=
"2"
>This is Mode=OneWay. </
TextBlock
>
Below is the code-behind.
2
namespace
DataBindingCSharpCorner1Way
4
public
partial
class
MainWindow : Window
6
Employee employee =
new
Employee
8
EmployeeNumber =
"217"
,
12
Department =
"Investigations"
16
InitializeComponent();
17
this
.DataContext = employee;
19
private
void
Button_Click(
object
sender, RoutedEventArgs e)
21
string
message = employee.FirstName +
" is a "
+ employee.Title;
22
MessageBox.Show(message,
"One Way"
);
26
public
string
EmployeeNumber {
get
;
set
; }
27
public
string
FirstName {
get
;
set
; }
28
public
string
LastName {
get
;
set
; }
29
public
string
Title {
get
;
set
; }
30
public
string
Department {
get
;
set
; }
Two Way XAML
With Two-Way the user is able to enter in their own text and when they click the Show button they see a message box with their own text in it.
1
<
Window
x:Class
=
"DataBindingCSharpCorner2Way.MainWindow"
6
xmlns:local
=
"clr-namespace:DataBindingCSharpCorner2Way"
8
WindowStartupLocation
=
"CenterScreen"
9
Title
=
"DataBindingCSharpCorner2Way"
Height
=
"150"
Width
=
"370"
>
12
<
RowDefinition
Height
=
"Auto"
/>
13
<
RowDefinition
Height
=
"Auto"
/>
14
<
RowDefinition
Height
=
"*"
/>
15
</
Grid.RowDefinitions
>
16
<
Grid.ColumnDefinitions
>
17
<
ColumnDefinition
Width
=
"Auto"
/>
18
<
ColumnDefinition
Width
=
"200"
/>
19
</
Grid.ColumnDefinitions
>
20
<
Label
Name
=
"nameLabel"
Margin
=
"2"
>_Name:</
Label
>
21
<
TextBox
Name
=
"nameText"
Grid.Column
=
"1"
Margin
=
"2"
Text
=
"{Binding FirstName, Mode = TwoWay}"
/>
22
<
Label
Name
=
"titleLabel"
Margin
=
"2"
Grid.Row
=
"1"
>_Title:</
Label
>
23
<
TextBox
Name
=
"titleText"
Grid.Column
=
"1"
Grid.Row
=
"1"
Margin
=
"2"
Text
=
"{Binding Title, Mode = TwoWay}"
/>
24
<
StackPanel
Grid.Row
=
"2"
Grid.ColumnSpan
=
"2"
>
25
<
Button
Content
=
"_Show..."
Click
=
"Button_Click"
/>
Below is the C#.
2
namespace
DataBindingCSharpCorner2Way
4
public
partial
class
MainWindow : Window
6
Employee employee =
new
Employee
8
EmployeeNumber =
"217"
,
12
Department =
"Investigations"
16
InitializeComponent();
17
this
.DataContext = employee;
19
private
void
Button_Click(
object
sender, RoutedEventArgs e)
21
string
message = employee.FirstName +
" is a "
+ employee.Title;
22
MessageBox.Show(message,
"Two Way"
);
26
public
string
EmployeeNumber {
get
;
set
; }
27
public
string
FirstName {
get
;
set
; }
28
public
string
LastName {
get
;
set
; }
29
public
string
Title {
get
;
set
; }
30
public
string
Department {
get
;
set
; }
Series Navigation << WPF DataContext StaticResource