前言
Prism默认的绑定规则是Views文件夹内的界面(如Test.xaml)查找ViewModels文件夹中对应的ViewModel(如TestViewModel.xaml), 所以ViewModel的后缀必须正确,如需要修改默认规则,请在App中重写方法ConfigureViewModelLocator
如果需要对自定义ViewModel进行绑定,请在ConfigureViewModelLocator方法中加入代码
//Test为自定义的ViewModel类
ViewModelLocationProvider.Register<MainWindow, Test>();
一、数据绑定
分别创建Views和ViewModels文件夹,并创建窗体及ViewModel类
xaml代码,绑定TextBox的文本
prism:ViewModelLocator.AutoWireViewModel,为True时表示自动关联ViewModel
<Window x:Class="PrismTestDemo.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="350" Width="525" >
<Grid>
<TextBox Width="120" Height="30" Text="{Binding NameText}"></TextBox>
</Grid>
</Window>
ViewModel代码:
安装Prism Template Pack扩展后可以简化属性的定义,propp
public class MainWindowViewModel : BindableBase
{
private string _nameText;
public string NameText
{
get { return _nameText; }
set { SetProperty(ref _nameText, value); }
}
public MainWindowViewModel()
{
this.NameText = "Hello Prism";
}
}
二、命令
1.简单的命令
Prism使用DelegateCommand类型定义命令
在xaml中添加一个按钮用于触发命令
<Window x:Class="PrismTestDemo.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Height="350" Width="525" >
<StackPanel VerticalAlignment="Center">
<TextBox Width="120" Height="30" Text="{Binding NameText}"></TextBox>
<Button Width="120" Height="30" Command="{Binding ButtonCommand}">按钮</Button>
</StackPanel>
</Window>
ViewModel代码:
public class MainWindowViewModel : BindableBase
{
private string _nameText;
public string NameText
{
get { return _nameText; }
set { SetProperty(ref _nameText, value); }
}
private DelegateCommand _buttonCommand;
public DelegateCommand ButtonCommand =>
_buttonCommand ?? (_buttonCommand = new DelegateCommand(ExecuteButtonCommand, CanExecuteButtonCommand));
public MainWindowViewModel()
{
this.NameText = "Hello Prism";
}
void ExecuteButtonCommand()
{
this.NameText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private bool CanExecuteButtonCommand()
{
return true;
}
}
CanExecuteButtonCommand代表此命令是否可执行,返回的布尔值默认绑定命令所属控件的IsEnable属性
安装Prism Template Pack扩展后可以简化命令创建,cmd
2.带参命令
xaml中加入按钮传入命令参数:
<Button Width="120" Height="30" Command="{Binding ParamCommand}" CommandParameter="我是参数">带参命令</Button>
ViewModel代码:
private DelegateCommand<string> _paramCommand;
public DelegateCommand<string> ParamCommand =>
_paramCommand ?? (_paramCommand = new DelegateCommand<string>(ExecuteParamCommand));
void ExecuteParamCommand(string parameter)
{
MessageBox.Show(parameter);
}
使用快速命令cmdg
创建
3.事件转命令
只有继承了ICommandSource接口的控件才会拥有Command依赖属性,基本只有ButtonBase和MenuItem继承了ICommandSource,所以像Button、RadioButton、CheckBox都有Command属性,但是我们常见的一些TextBox、ComboBox等就没有Command属性,那这种情况我们该如何绑定命令?
首先在xaml引入如下dll
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
我们在ComboBox控件绑定SelectionChanged事件,写法如下:
<ComboBox x:Name="cmb" ItemsSource="{Binding DataList}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelChangedCommand}" CommandParameter="{Binding ElementName=cmb,Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
ViewModel代码:
private DelegateCommand<object> _selChangedCommand;
public DelegateCommand<object> SelChangedCommand =>
_selChangedCommand ?? (_selChangedCommand = new DelegateCommand<object>(ExecuteSelChangedCommand));
void ExecuteSelChangedCommand(object parameter)
{
MessageBox.Show(parameter?.ToString());
}