WPF之MVVM模式,通过委托两个页面间通信

本文介绍如何在WPF应用中使用MVVM模式,通过委托实现在MainWindow的ViewModel与UserControlB之间的通信。详细阐述了Command.cs、UserControlB.xaml及其后台代码、UserControlBViewModel.cs、以及MainWindow的XAML和后台代码、MainWindowViewModel.cs的实现过程,展示了具体的事件处理和数据绑定方法。
摘要由CSDN通过智能技术生成

需求:MVVM模式,在MainWindow的ViewModel接收到UserCOntrolB的Command事件

Command .cs

using System;
using System.Windows.Input;

namespace WpfApplication
{
    public class Command : ICommand
    {
        Action<object> executeMethod;
        Func<object, bool> canExecuteMethod;
        public Command(Action<object> execute):this(execute,null)
        {

        }
        public Command(Action<object> execute, Func<object, bool> canExecute)
        {
            executeMethod = execute;
            canExecuteMethod = canExecute;
        }
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return canExecuteMethod(parameter);
        }

        public void Execute(object parameter)
        {
            executeMethod(parameter);
        }
    }
}

UserControlB.xaml

<UserControl x:Class="WpfApplication.UserControlB"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.micr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值