Prism8.0(二):数据绑定与命令

阅读: 评论:0

Prism8.0(二):数据绑定与命令

Prism8.0(二):数据绑定与命令

前言

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=""xmlns:x=""xmlns:prism="/"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=""xmlns:x=""xmlns:prism="/"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=""

我们在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());
}

官方文档:.html

本文发布于:2024-01-31 20:04:31,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170670267331029.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:绑定   命令   数据
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23