Interestingly, the point to start the update
First Data Binding One of the Element Binding
See examples
-
<Window x:Class=
"WpfApplication20.MainWindow"
-
xmlns=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
xmlns:x=
"http://schemas.microsoft.com/winfx/2006/xaml"
-
Title=
"MainWindow" Height=
"156.383" Width=
"246.489" x:Name=
"MWindow">
-
<Grid>
-
<TextBox HorizontalAlignment=
"Left" Height=
"23" Margin=
"30,48,0,0" TextWrapping=
"Wrap" VerticalAlignment=
"Top" Width=
"120" Text=
"{Binding ElementName=MWindow,Path=PresonName}"/>
-
<TextBlock HorizontalAlignment=
"Left" Margin=
"30,16,0,0" TextWrapping=
"Wrap" VerticalAlignment=
"Top" Width=
"42" Text=
"部门:"/>
-
<Button Content=
"提交" HorizontalAlignment=
"Left" Margin=
"30,85,0,0" VerticalAlignment=
"Top" Width=
"75" Click=
"Button_Click"/>
-
<Button Content=
"重置" HorizontalAlignment=
"Left" Margin=
"127,85,0,0" VerticalAlignment=
"Top" Width=
"75" Click=
"Button_Click_1"/>
-
<TextBlock HorizontalAlignment=
"Left" Margin=
"85,16,0,0" TextWrapping=
"Wrap" VerticalAlignment=
"Top" Text=
"{Binding ElementName=MWindow,Path=Department}"/>
-
-
</Grid>
-
</Window>
-
public
partial
class
MainWindow :
Window,
INotifyPropertyChanged
-
{
-
public MainWindow()
-
{
-
InitializeComponent();
-
}
-
-
private
string department;
-
-
public
string Department
-
{
-
get {
return
"软件开发"; }
-
-
}
-
private
string presonName;
-
-
public
string PresonName
-
{
-
get {
return presonName; }
-
set { presonName =
value;
-
OnPropertyChanged(
"PresonName");
-
}
-
}
-
private void Button_Click(object sender, RoutedEventArgs e)
-
{
-
MessageBox.Show(
"Hi," + PresonName);
-
}
-
-
private void Button_Click_1(object sender, RoutedEventArgs e)
-
{
-
PresonName =
"";
-
}
-
-
#region INotifyPropertyChanged 成员
-
-
public
event PropertyChangedEventHandler PropertyChanged;
-
public void OnPropertyChanged(string name)
-
{
-
if(PropertyChanged!=
null){
-
PropertyChanged.Invoke(
this,
new PropertyChangedEventArgs(name));
-
}
-
}
-
-
#endregion
-
}
We have two operations in this example
A data binding
A data notification
Element Binding is specified window because the window after window to create a form has this attribute by attribute name. How properties
Notification needs to inherit INotifyPropertyChanged
Implement an interface method
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if(PropertyChanged!=null){
PropertyChanged.Invoke(this,new PropertyChangedEventArgs(name));
}
}
In the properties need to be informed of the following plus
OnPropertyChanged ( "" property name "");
They can play the role of the notification