西安网站建设公司云网,网站建设商务通什么意思,提供常州网站建设,大连弗莱科技官方网站在Silverlight中的MVVM模式下将前台页面和ViewModel界面交互分离开是通过本节所要讲述的Command实现的。我们自定义一个Command需要继承于ICommand接口并且实现这个接口。它有CanExecute()、Execute()方法和CanExecuteChanged事件组成。 CanExecute()#xff1a;判断是否继续执… 在Silverlight中的MVVM模式下将前台页面和ViewModel界面交互分离开是通过本节所要讲述的Command实现的。我们自定义一个Command需要继承于ICommand接口并且实现这个接口。它有CanExecute()、Execute()方法和CanExecuteChanged事件组成。 CanExecute()判断是否继续执行操作。 Execute()执行操作的内容。 CanExecuteChanged当出现影响是否应执行该命令的更改时发生。 首先自定义的一个btnCommand。 public class btnCommand:ICommand{
private bool canExe;
/// summary
/// 构造函数设置是否执行操作
/// /summary
/// param namecanexe/parampublic btnCommand(bool canexe){
this.canExe canexe; }
/// summary
/// 判断是否执行操作
/// /summary
/// param nameparameter/param
/// returns/returnspublic bool CanExecute(object parameter){
if (canExe){
return true; }
return false;}
/// summary
/// 是否执行操作的变更发生时
/// /summarypublic event EventHandler CanExecuteChanged;
/// summary
/// 执行操作的内容可以变为Action行为
/// /summary
/// param nameparameter/parampublic void Execute(object parameter){
if (parameter ! null){ MessageBox.Show(parameter.ToString()); }
else{MessageBox.Show(未设置CommandParameter);}}} 其次定义一个ViewModel并且在构造函数中初始化两个Command属性。 public class BtnViewModel{
// 设置两个命令public ICommand BtnCommand { get; set; }
public ICommand BtnCommandTrue { get; set; }
public BtnViewModel(){
//初始化两个命令值BtnCommand new btnCommand(false);BtnCommandTrue new btnCommand(true);}} 再次将ViewModel初始化为页面数据源 UserControl.DataContextlocal:BtnViewModel //UserControl.DataContext 最后前台的两个按钮绑定Command Grid x:NameLayoutRoot BackgroundWhiteButton Content第一个 Height23 HorizontalAlignmentLeftCommand{Binding BtnCommand} CommandParameter第一个CommandMargin94,80,0,0 Namebutton1 VerticalAlignmentTop Width75 /Button Command{Binding BtnCommandTrue} CommandParameter第二个Command Content第二个 Height23 HorizontalAlignmentLeft Margin94,140,0,0Namebutton2 VerticalAlignmentTop Width75 //Grid 如需源码请点击 SLICommand.zip 下载下面是效果图。有一个按钮CanExecute有一个不能点击。 转载于:https://blog.51cto.com/chengxingliang/835766