寻常使用可能就是知道其绑定的command命令
正常命名绑定
- <Button Content="DelegateCommand" Command="{Binding Command}" Margin="10"/>
后端
- public DelegateCommand Command { get; }
构造函数中
- Command = new DelegateCommand(Execute);
1使用 DelegateCommand命令绑定参数二 委托的bool表达式
参数二
是一个委托函数 bool 类型的返回值 无参函数 也能称呼为检查函数 每当在属性set中加上了 命令名称+ .RaiseCanExecuteChanged();就会再设置当前新值的时候触发当前方法CanExecute (触发指定的检查方法,方法为您自己再命令创建之初所指定的bool 返回值 无参的方法 )
当然写法 lamba表达式写法可以
2 RaiseCanExecuteChanged 触发检查函数
命令名称+ .RaiseCanExecuteChanged(); 触发检查方法 指定的前提下
同上就是当IsEnabled 为false 的时候绑定command命令的控件将不可用 为true的时候为可用
检查方法 返回的ture 或 false 会是当前command 是否能用的关键
3使用ObservesProperty 监听属性(含监听多个属性)
加上了ObservesProperty 方法并指定了一个属性或多个属性 被指定的属性我们称呼我为被监听的属性 每当监听的属性发生改变的时候那么就会触发检查函数 当然您需要有指定检查函数
示例
- ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyProperty);
- //指定了检查的方法,然后再用方法指定一个监听的属性。
- //每当这个属性的值 发生更改的时候我们就会去触发一次检查的方法CanExecute。
- // 有了ObservesProperty当前这个方法指定监听属性之后。不需要再给属性写上。 ExecuteDelegateCommand.RaiseCanExecuteChanged();
- ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyProperty).ObservesProperty(() => LL);
- //与上不同是设置了多个监听属性
4 使用 ObservesCanExecute 可以不用指定检查方法
它与ObservesProperty 监听属性一样与之不同的是它指定监听的属性后可以不用指定检查方法 被指定的属性也不用写触发检查方法的语句
5带参数的命令 接收传递参数
- ExecuteDelegateCommands = new DelegateCommand<string>(ExecuteGeneric).ObservesCanExecute(() => IsEnabled);
ExecuteGeneric 方法将只能有一个参数 想知道为什么可以看F12源码
CommandParameter 是传递的参数
Obj是 结果 也就是6
源码 前端
<Window
x:Class="Prismcommand.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Prismcommand"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<StackPanel>
<CheckBox
Margin="10"
Content="IsEnabled"
IsChecked="{Binding IsEnabled}" />
<CheckBox
Margin="10"
Content="MyProperty"
IsChecked="{Binding MyProperty}" />
<CheckBox
Margin="10"
Content="LL"
IsChecked="{Binding LL}" />
<Button
Margin="10"
Command="{Binding ExecuteDelegateCommand}" CommandParameter="6"
Content="DelegateCommand" />
<TextBlock
Margin="10"
FontSize="30"
Text="{Binding UpdateText}" />
</StackPanel>
</Grid>
</Window>
后端 VIewModel
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Prismcommand
{
public class Class1 : BindableBase
{
#region MyRegion
private string _Title = "Hello";
public string Title
{
get { return _Title; }
set { SetProperty(ref _Title, value); }
}
private string _updateText;
public string UpdateText
{
get { return _updateText; }
set { SetProperty(ref _updateText, value); }
}
#endregion
private bool _IsEnabled;
public bool IsEnabled
{
get { return _IsEnabled; }
set{ SetProperty(ref _IsEnabled, value);}
// ExecuteDelegateCommand.RaiseCanExecuteChanged();
//当命令实例化没有加Observes方法时候,必须要这个,如果有添加Observes方法时候,可以去掉这个
}
private bool myVar;
public bool MyProperty
{
get { return myVar; }
set { SetProperty(ref myVar, value);}
}
private bool ll;
public bool LL
{
get { return ll; }
set
{
ll = value; RaisePropertyChanged();
// ExecuteDelegateCommand.RaiseCanExecuteChanged();//启动command状态检查。
}
}
public DelegateCommand ExecuteDelegateCommand { get; }
public DelegateCommand<string> ExecuteDelegateCommands { get; }
#region MyRegion
//public DelegateCommand DelegateCommandObservesProperty { get; }
//public DelegateCommand DelegateCommandObservesCanExecute { get; }
//public DelegateCommand<string> ExecuteGenericDelegateCommand { get; }
//public ICommand command { get => new DelegateCommand(Execute).ObservesProperty(() => IsEnabled); }
#endregion
public Class1()
{
// ExecuteDelegateCommand = new DelegateCommand(Execute);
// ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute);
//当前CanExecute方法是检查当前command状态是否可用。
//ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyProperty);
指定了检查的方法,然后再用方法指定一个监听的属性。
每当这个属性的值 发生更改的时候我们就会去触发一次检查的方法CanExecute。
有了ObservesProperty当前这个方法指定监听属性之后。不需要再给属性写上。 ExecuteDelegateCommand.RaiseCanExecuteChanged();
//ExecuteDelegateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyProperty).ObservesProperty(() => LL);
与上不同是设置了多个监听属性
// ExecuteDelegateCommand = new DelegateCommand(Execute).ObservesCanExecute(() => LL);
//当前LL就是被监听的属性 被监听的属性不需要写触发检查的方法 ExecuteDelegateCommand.RaiseCanExecuteChanged()
//也可以不写检查方法。 默认检查方法都是返回监听的这个属性
ExecuteDelegateCommands = new DelegateCommand<string>(ExecuteGeneric).ObservesCanExecute(() => IsEnabled);
}
private void ExecuteGeneric(string obj)
{
UpdateText = obj;
}
private bool CanExecute()//当然,这个方法如果返回为true,那么当前这个命令。 控件就是可以用的。reinforce当前绑定命令的控件不可用。
{
return IsEnabled;
}
private void Execute()
{
UpdateText = $"Updated:{DateTime.Now.ToString()}";
}
}
}