1. 程式人生 > 其它 >Prism8.0(二):資料繫結與命令

Prism8.0(二):資料繫結與命令

技術標籤:PrismWPFc#wpfxamlprism

前言

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