需求: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=

本文介绍如何在WPF应用中使用MVVM模式,通过委托实现在MainWindow的ViewModel与UserControlB之间的通信。详细阐述了Command.cs、UserControlB.xaml及其后台代码、UserControlBViewModel.cs、以及MainWindow的XAML和后台代码、MainWindowViewModel.cs的实现过程,展示了具体的事件处理和数据绑定方法。
最低0.47元/天 解锁文章
8970

被折叠的 条评论
为什么被折叠?



