Part 2 travel to the internal world of WPF
Chapter 2 Introduction to Binding
6.1 status of Data Binding in WPF
Applications have a three-tier structure, namely the data storage layer, data processing layer, and data presentation layer. The storage layer is equivalent to a city's storage zone, which consists of a database and a file system. The processing layer should be called the logic layer more correctly, algorithms related to business logic and used for data processing are concentrated here; the function of the presentation layer is to display processed data to users through a visual interface or other types of interfaces to other applications.
The essence of a program is the data plus algorithm.
6.2 Binding Basics
[Csharp]
Using System. ComponentModel;
Namespace FirstWpfApplication. Objects
{
Class Student: INotifyPropertyChanged
{
Private string name;
Public string Name
{
Get {return this. name ;}
Set
{
This. name = value;
// Trigger the System. ComponentModel. INotifyPropertyChanged. PropertyChanged event
PropertyChanged. Invoke (this, new PropertyChangedEventArgs ("Name "));
}
}
Public event PropertyChangedEventHandler PropertyChanged;
}
}
Using System. ComponentModel;
Namespace FirstWpfApplication. Objects
{
Class Student: INotifyPropertyChanged
{
Private string name;
Public string Name
{
Get {return this. name ;}
Set
{
This. name = value;
// Trigger the System. ComponentModel. INotifyPropertyChanged. PropertyChanged event
PropertyChanged. Invoke (this, new PropertyChangedEventArgs ("Name "));
}
}
Public event PropertyChangedEventHandler PropertyChanged;
}
}
[Html]
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" BorderBrush = "Black" Grid. Row = "0"/>
<TextBox x: Name = "textBox2" BorderBrush = "Black" Grid. Row = "2"/>
<Button x: Name = "button1" Content = "OK" Click = "button#click" Grid. Row = "4"/>
</Grid>
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" BorderBrush = "Black" Grid. Row = "0"/>
<TextBox x: Name = "textBox2" BorderBrush = "Black" Grid. Row = "2"/>
<Button x: Name = "button1" Content = "OK" Click = "button#click" Grid. Row = "4"/>
</Grid>
[Csharp]
Public partial class MainWindow: Window
{
Student stu;
Public MainWindow ()
{
InitializeComponent ();
Stu = new Student ();
Binding binding = new Binding ();
Binding. Source = stu;
Binding. Path = new PropertyPath ("Name ");
BindingOperations. SetBinding (this. textBox1, TextBox. TextProperty, binding );
}
Private void button#click (object sender, RoutedEventArgs e)
{
Stu. Name = textBox2.Text;
}
}
Public partial class MainWindow: Window
{
Student stu;
Public MainWindow ()
{
InitializeComponent ();
Stu = new Student ();
Binding binding = new Binding ();
Binding. Source = stu;
Binding. Path = new PropertyPath ("Name ");
BindingOperations. SetBinding (this. textBox1, TextBox. TextProperty, binding );
}
Private void button#click (object sender, RoutedEventArgs e)
{
Stu. Name = textBox2.Text;
}
}