WPF绑定数据
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="winMain" Title="MainWindow" Height="350" Width="525"> <Grid> <StackPanel> <TextBox x:Name="txt" Text="{Binding Name}" Width="200" Height="20"></TextBox> <Button Width="80" Height="20" Content="Button" Click="Button_Click" ></Button> </StackPanel> </Grid> </Window> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public partial class MainWindow : Window { private Person _Person = null; public Person Person { get { return this._Person; } set { this._Person = value; } } public MainWindow() { InitializeComponent(); this._Person = new Person() { Name = "123" }; this.txt.DataContext = this.Person; } private void Button_Click(object sender, RoutedEventArgs e) { this._Person.Name = "456"; } } public class Person : INotifyPropertyChanged { private string _Name; public string Name { get { return this._Name; } set { this._Name = value; this.OnPropertyChanged("Name"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } |
浙公网安备 33010602011771号