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

2 篇文章 2 订阅
前言

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

官方文档:https://prismlibrary.com/docs/index.html

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Prism 8.0是一款强大的统计和绘图软件,主要用于数据分析、图表绘制和科学研究。如果你想要下载Prism 8.0软件,有几种可行的方法。 首先,你可以访问官方网站下载。在你喜欢的搜索引擎中搜索“Prism 8.0官方网站”,点击进入官方网站。在网站的首页或者下载页面,你应该能找到一个下载链接。点击链接,根据指示完成下载和安装过程。 其次,你也可以通过其他可信的软件下载网站下载Prism 8.0。在搜索引擎中搜索“Prism 8.0下载”,会显示很多软件下载网站的搜索结果。打开一个可信的网站,找到Prism 8.0的下载页面,点击下载链接进行下载。但是请注意,选择下载源时要小心,确保下载的文件不含有恶意软件或病毒。 另外,如果你是从学术或科研机构中使用Prism 8.0,你可能可以从你的机构的软件资源中心或者类似的平台上下载和安装Prism 8.0。这样你就可以使用你的机构提供的许可证进行软件的安装和注册。 无论你选择哪种方式下载Prism 8.0,都需要确保你的电脑系统满足软件的最低要求,并且遵循软件的安装指南。下载完毕后,你就可以开始使用Prism 8.0进行数据分析和绘图了。祝你成功! ### 回答2: Prism 8.0是一款功能强大的统计软件,用于分析和可视化数据。要下载Prism 8.0软件,首先需要前往官方网站,例如GraphPad软件的官网。 在网站的主页上,可以找到一个下载页或者产品页,通常会有一个“下载”按钮。点击这个按钮后,系统会提供一个下载链接,你可以选择下载的操作系统,如Windows、Mac或Linux。点击相应的操作系统后,会开始自动下载Prism 8.0软件的安装文件。 安装文件下载完成后,双击它运行,启动安装向导。按照向导的指引,选择你想安装的语言,阅读并同意软件的许可协议,选择安装目录等。然后,点击“下一步”或“安装”按钮,等待安装程序完成。 安装完成后,你就可以在计算机上找到Prism 8.0软件的快捷方式,双击它来启动软件。第一次启动时,可能会要求输入许可证密钥,如果你已经购买了合法的许可证,可以在相应的位置输入密钥进行激活。 在激活完成后,Prism 8.0软件就可以正常使用了。你可以通过导入已有的数据或新建数据来进行分析和可视化操作,利用Prism 8.0的各种统计方法和图表功能,为你的实验结果提供可靠的支持。 总之,要下载Prism 8.0软件,需要前往官方网站,根据操作系统选择下载,然后按照安装向导进行安装,最后激活软件即可开始使用。希望这个回答对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值