添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在WPF中使用MVVM框架时,我们常常会使用命令(Command)来实现视图(View)和视图模型(ViewModel)之间的交互。而参数的传递是命令中的一个重要部分。下面我将详细介绍如何在WPF的MVVM框架中传递命令参数,包括单个参数和多个参数的传递。

单个参数的传递:
在WPF中,我们可以通过CommandParameter属性来传递单个参数。例如,我们有一个按钮,当点击这个按钮时,我们希望传递一个字符串参数给视图模型中的某个方法。以下是具体的代码示例:

<!-- View -->
<UserControl x:Class="MyNamespace.View"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:prism="http://prismlibrary.com/">
    <Button Content="Click Me" Command="Binding CommandDoSomething" CommandParameter="Hello World"/>
</UserControl>

在这个例子中,我们有一个按钮,其Command属性绑定到了视图模型中的CommandDoSomething方法。而CommandParameter属性则绑定了我们希望传递的字符串参数"Hello World"。

在视图模型中,我们可以这样接收这个参数:

// ViewModel
public class ViewModel : Screen
    public ViewModel()
        CommandDoSomething = new DelegateCommand<string>(DoSomething);
    private void DoSomething(string argument)
        Debug.WriteLine($"DoSomething called with argument: {argument}");

多个参数的传递:
首先,我们需要创建一个实现了ICommand接口的自定义Command类,例如:

public class MyCommand : ICommand
    private Action<object> _execute;
    private Func<object, bool> _canExecute;
    public MyCommand(Action<object> execute, Func<object, bool> canExecute = null)
        _execute = execute;
        _canExecute = canExecute;
    public event EventHandler CanExecuteChanged;
    public bool CanExecute(object parameter)
        return _canExecute?.Invoke(parameter) ?? true;
    public void Execute(object parameter)
        _execute?.Invoke(parameter);

然后,在ViewModel中定义一个属性来存储Command,并在构造函数中初始化该Command,例如:

public class MyViewModel : INotifyPropertyChanged
    private ICommand _myCommand;
    public ICommand MyCommand
        get { return _myCommand; }
            _myCommand = value; 
            OnPropertyChanged(nameof(MyCommand)); 
    public MyViewModel()
        MyCommand = new MyCommand(ExecuteCommand);
    private void ExecuteCommand(object parameter)
        // 在这里处理Command的逻辑,可以获取到传入的参数
        string message = parameter as string;//这里既可以传单个参数也可以传多个参数,对object做相应的转化即可!!!!
        // 执行相应的操作
    // INotifyPropertyChanged 接口的实现...

最后,在XAML中将Button与Command绑定,并传递参数,例如:

<Button Content="Click Me" Command="{Binding MyCommand}" CommandParameter="Hello, World!" />

这样,当点击按钮时,Command将会被执行,同时参数"Hello, World!"将传递给Command的Execute方法(既可以传单个参数也可以传多个参数,对object做相应的转化即可)。

1.Model:Model就是一个class,是对现实中事物的抽象,开发过程中涉及到的事物都可以抽象为Model,例如客户,客户的姓名、编号、电话、住址等属性也对应了class中的Property,客户的下订单、付款等行为对应了class中的方法。 2. View:View很好理解,就是视图界面。 3. ViewModel:上面说过Model抽象,那么ViewModel就是对View的抽象。显示的数据对应着ViewMode中的Property,执行的命令对应着ViewModel中的Command。 (通过一个例子来解释MVVM 让我使用MVVM创建一个简单的用户管理应用程序。按部就班我们 微软对PlacementTarget的解释是:获取或设置UIElement,当它打开相对于它确定ContextMenu的位置。ContextMenu无论定义在.cs或.xaml文件中,都不继承父级的DataContext,所以如果要绑定父级的DataContext,直接DataContext=“{Binding}”是行不通的。(Button.Command 和 ContextMenu.IsOpen 的绑定部分可以不关注,这两个绑定是用来控制ContextMenu打开的)不能绑父级,但是能绑资源。 前言:在WPF中,将命令绑定到一个Button的Command属性中是非常简单的事情,例如: <Button Content="单击命令" Command="{Binding ClickCommand}"/> 但是很多候,我们要实现其它事件的命令绑定,以此实现彻底的MVVM开发模式,那就得用到微软另外一个神器了!它就是程序集System.Windows.Interactivity,在网上可以下载System.Windows.Interactivity.dll这个库文件。 当然,在mvvmli <Button Content="操作" Command="{Binding 命令名称}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=SelectedItem}" /> RelativeSource可以将某个控件作为参数转递给Command里面; 它有三个常用属性 //RelativeSou <Window x:Class="WpfApp03.TestWin07" 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"