Prism8.0 (2): データバインディングとコマンド

序文

Prism のデフォルトのバインディング ルールは、Views フォルダー内のインターフェイス (Test.xaml など) から ViewModels フォルダー内の対応する ViewModel (TestViewModel.xaml など) を検索することであるため、ViewModel のサフィックスは正しい必要があります。デフォルトのルールを変更するには、「アプリのメソッドのオーバーライド」に進んでくださいConfigureViewModelLocator
。カスタム ViewModel をバインドする必要がある場合は、ConfigureViewModelLocator メソッドにコードを追加してください。

//Test为自定义的ViewModel类
ViewModelLocationProvider.Register<MainWindow, Test>();

1. データバインディング

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";
    }
}

2. 注文

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>

ビューモデルコード:

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>

ビューモデルコード:

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"

SelectionChanged イベントを ComboBox コントロールにバインドし、次のように記述します。

<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>

ビューモデルコード:

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

おすすめ

転載: blog.csdn.net/qq_29242649/article/details/113695962