命令就是Command
1.建立MyCommand类,继承ICommand,并且实现它的接口方法,(1个事件,2个方法)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WpfApp1
{
class MyCommand : ICommand
{
//一个事件,2个方法
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
throw new NotImplementedException();
}
public void Execute(object parameter)
{
throw new NotImplementedException();
}
}
}
对命令执行的方法进行补充,可以有多种带参数的方法等等。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WpfApp1
{
public class MyCommand : ICommand
{
//一个事件,2个方法
/// <summary>
/// 发生变化时,会触发的事件(必须要实现)
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
/// 不返回参数的方法
/// </summary>
public Action ExecuteAction0 { get; set; }
/// <summary>
/// 一个参数,不返回参数的方法
/// </summary>
public Action<object> ExecuteAction { get; set; }
/// <summary>
/// 一个参数,返回一个参数的方法
/// </summary>
public Func<object, bool> CanExecuteAction { get; set; }
/// <summary>
/// 能做吗,一般多是true(必须要实现)
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
if (CanExecuteAction != null)//这里先进行判断,true就是执行方法,false就是不执行方法,一般都是true。
return CanExecuteAction(parameter);
return true; //这里和MainViewModel中的MyCom.CanExecuteAction = new Func<object, bool>(this.CanDoSomething)都可以进行判断。
}
/// <summary>
/// 做什么(必须要实现)
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
//ExecuteAction0();//这里执行的是,不返回参数的方法。
ExecuteAction?.Invoke(parameter);//这里执行的是,带一个参数,不返回参数的方法。
}
}
}
2.建立业务类,MainViewModel类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp1
{
public class MainViewModel
{
//先实例化这个命令(这是属于ViewModel的命令,等下要被送到View中去)
public MyCommand MyCom { get; set; }
public MainViewModel()
{
//在ViewModel的构造函数中,完成对命令的设置
MyCom = new MyCommand();
MyCom.CanExecuteAction = new Func<object, bool>(this.CanDoSomething);//命令执行先进行判断,这里和MyCommand中的CanExecute都可以进行判断。
MyCom.ExecuteAction = new Action<object>(this.DoSomething);//执行命令,带一个参数
//MyCom.ExecuteAction0 = new Action(this.DoSomething0);//执行命令,不带参数
}
public void DoSomething(object param)
{
MessageBox.Show("123" + param);//执行方法,返回界面的命令带的参数
}
public void DoSomething0()
{
MessageBox.Show("123");//执行方法,返回界面的命令带的参数
}
public bool CanDoSomething(object param)
{
//这里可以和界面进行数据交互,进行判断。判断能否做这个事情,大部分时候返回true就行了,返回false,方法就不执行了。
return true;
}
}
}
3.在MainWindow中关联上MainViewModel。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
4. 在界面上调用命令
代码
<Window x:Class="WpfApp1.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="222,185,0,0" Command="{Binding MyCom}" CommandParameter="{Binding ElementName=txt, Path=Text}" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="txt" HorizontalAlignment="Left" Height="23" Margin="222,128,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
5.效果
拓展
上面是传递单个值,下面说说多值传递的问题,传值有2种方法
第一种,建立一个对象,把对象当做整体传入
首先建立stu类
public class Stu
{
public int id { get; set; }
public string name { get; set; }
public int sex { get; set; }
}
然后初始化数据
前台界面进行绑定
测试,点击按钮,然后看传递的参数
第二种,使用多参数的方式传值
1.建立多值转换类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
namespace WpfApp1
{
public class ObjectConvert : IMultiValueConverter
{
#region IMultiValueConverter Members
public static object ConverterObject;
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values.ToArray();
}
public object[] ConvertBack(object value, Type[] targetTypes,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
2.对转化类进行引用
<Window.Resources>
<local:ObjectConvert x:Key="objectConverter"></local:ObjectConvert>
</Window.Resources>
3.界面中传值
4.测试 ,点击按钮,然后看传递的参数
补充其他代码
xaml
<Window x:Class="WpfApp1.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:ObjectConvert x:Key="objectConverter"></local:ObjectConvert>
</Window.Resources>
<Grid>
<!--<Button Content="Button" HorizontalAlignment="Left" Margin="222,185,0,0" Command="{Binding MyCom}" CommandParameter="{Binding ElementName=txt, Path=Text}" VerticalAlignment="Top" Width="75"/>-->
<!--<Button Content="Button" HorizontalAlignment="Left" Margin="222,185,0,0" Command="{Binding MyCom}" CommandParameter="{Binding a}" VerticalAlignment="Top" Width="75"/>-->
<Button Content="Button" HorizontalAlignment="Left" Margin="222,185,0,0" Command="{Binding MyCom}" VerticalAlignment="Top" Width="75">
<Button.CommandParameter>
<MultiBinding Converter="{ StaticResource ResourceKey=objectConverter}">
<Binding ElementName="txt"></Binding><!--传整个控件-->
<Binding ElementName="txt_Copy" Path="Text"></Binding><!--传控件的值-->
<Binding Path="a" ></Binding><!--传对象-->
<Binding Source="自定义值" ></Binding> <!--传自定义的值-->
</MultiBinding>
</Button.CommandParameter>
</Button>
<TextBox x:Name="txt" HorizontalAlignment="Left" Height="23" Margin="222,128,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txt_Copy" HorizontalAlignment="Left" Height="23" Margin="412,172,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
MainViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp1
{
public class MainViewModel
{
public Stu a { get; set; }
//先实例化这个命令(这是属于ViewModel的命令,等下要被送到View中去)
public MyCommand MyCom { get; set; }
public MainViewModel()
{
//在ViewModel的构造函数中,完成对命令的设置
MyCom = new MyCommand();
MyCom.CanExecuteAction = new Func<object, bool>(this.CanDoSomething);//命令执行先进行判断,这里和MyCommand中的CanExecute都可以进行判断。
MyCom.ExecuteAction = new Action<object>(this.DoSomething);//执行命令,带一个参数
//MyCom.ExecuteAction0 = new Action(this.DoSomething0);//执行命令,不带参数
a = new Stu { id = 1, name = "张三", sex=0 };
}
public void DoSomething(object param)
{
MessageBox.Show("123" + param);//执行方法,返回界面的命令带的参数
}
public void DoSomething0()
{
MessageBox.Show("123");//执行方法,返回界面的命令带的参数
}
public bool CanDoSomething(object param)
{
//这里可以和界面进行数据交互,进行判断。判断能否做这个事情,大部分时候返回true就行了,返回false,方法就不执行了。
return true;
}
public class Stu
{
public int id { get; set; }
public string name { get; set; }
public int sex { get; set; }
}
}
}