wpf数据绑定
# 控件和控件的绑定
<Slider Margin="5" x:Name="slider"></Slider>
<TextBox Text="{Binding ElementName=slider,Path=Value}" Margin="5" Height="30"/>
<TextBox Text="{Binding ElementName=slider,Path=Value, Mode=OneTime}" Margin="5" Height="30"/>
1
2
3
2
3
ElementName
: 要绑定哪个元素上面Path
: 绑定的属性是什么Mode
: 绑定模式Default
: 默认,双向绑定,都会发生变化OneTime
: 只绑定一次,后面绑定值发生变化不会跟着变化OneWay
: 单向绑定,绑定值发生变化会跟着变化,自身变化不会修改绑定值OneWayToSource
: 和OneWay
相反,自身变化会修改绑定值,绑定值发生变化不会影响自身TowWay
: 双向绑定
# 属性值的绑定
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 数据上下文
this.DataContext = new Test()
{
Name="张三"
};
}
}
public class Test
{
public string Name { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<TextBox Text="{Binding Name}" Margin="5" Height="30"/>
1
# ICommand命令
创建MyCommand.cs
自定义ICommand
命令
public class MyCommand : ICommand
{
Action executeAction;
// 将方法通过构造函数传进来
public MyCommand(Action action)
{
executeAction = action;
}
public event EventHandler? CanExecuteChanged;
// 是否能够执行该命令
public bool CanExecute(object? parameter)
{
return true;
}
// 执行命令
public void Execute(object? parameter)
{
executeAction();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
创建个数据模型MainViewModel.cs
public class MainViewModel
{
public MainViewModel()
{
ShowCommand = new MyCommand(Show);
}
public MyCommand ShowCommand { get; set; }
public void Show()
{
MessageBox.Show("点击了按钮");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
// 将数据模型添加到数据上下文
this.DataContext = new MainViewModel();
}
1
2
3
4
5
6
2
3
4
5
6
MainWindow.xaml
<!-- 绑定命令 -->
<Button Command="{Binding ShowCommand}"></Button>
1
2
2
这样当点击按钮的时候,就会执行ShowCommand
绑定的方法
# 属性通知修改
要想通知属性进行修改必须实现INotifyPropertyChanged
接口
public class MainViewModel: NotifyPropertyChanged
{
public MainViewModel()
{
Name = "Hellow";
// ...
}
public string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
// 调用事件
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
// 实现接口的事件
public event PropertyChangedEventHandler? PropertyChanged;
public void Show()
{
Name = "点击了按钮";
// ...
}
}
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
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
[CallerMemberName]
特性获取调用者的名称
public class ViewModeBase:INotifyPropertyChanged{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName=""){
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
}
```c#
public class MainViewModel: INotifyPropertyChanged
{
// ...
public string Name
{
// ...
set
{
name = value;
// 调用事件,使用`[CallerMemberName]`特性自动获取名称
OnPropertyChanged();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
上次更新: 2023/12/12, 21:55:41