SL4 - MVVM - DelegateCommand/Action/Interaction/Triggers概括(ICommand)

///

/// DelegateCommand

///

public class DelegateCommand : ICommand

{

private Predicate < object > canExecute;

private Action < object > method;

///

/// Occurs when changes occur that affect whether the command should execute.

///

public event EventHandler CanExecuteChanged;

///

/// Initializes a new instance of the class.

///

/// The method.

public DelegateCommand( Action < object > method)

:

this (method, null )

{

}

///

/// Initializes a new instance of the class.

///

/// The method.

/// The can execute.

public DelegateCommand( Action < object > method, Predicate < object > canExecute)

{

this .method = method;

this .canExecute = canExecute;

}

///

/// Defines the method that determines whether the command can execute in its current state.

///

/// Data used by the command. If the command does not require data to be passed, this object can be set to null.

///

/// true if this command can be executed; otherwise, false.

///

public bool CanExecute( object parameter)

{

if (canExecute == null )

{

return true ;

}

return canExecute(parameter);

}

///

/// Defines the method to be called when the command is invoked.

///

/// Data used by the command. If the command does not require data to be passed, this object can be set to null.

public void Execute( object parameter)

{

method.Invoke(parameter);

}

///

/// Raises the event.

///

/// The instance containing the event data.

protected virtual void OnCanExecuteChanged( EventArgs e)

{

var canExecuteChanged = CanExecuteChanged;

if (canExecuteChanged != null )

canExecuteChanged(

this , e);

}

///

/// Raises the can execute changed.

///

public void RaiseCanExecuteChanged()

{

OnCanExecuteChanged(

EventArgs .Empty);

}

}


方法一:-----------------------------------------------------------------

           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:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           xmlns:ViewModel="clr-namespace:Silverlight40.Binding.Command"
           Title="Demo Page">
    
        
        
            
        

        
        
            
            
        
        
    

Code部分:-----------------------------------------------------------------------------

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Silverlight40.Binding.Command
{
    public class MyViewModel
    {
        // 声明一个 ICommand 类型,用于绑定到 ButtonBase 或 Hyperlink 的 Command 属性上
        public ICommand Hello { get; set; }
        public MyViewModel()
        {
            // 绑定了 Hello 的命令被执行时则会调用 ExecuteHello(object parameter) 方法
            Hello = new MyCommand(ExecuteHello);
        }
        private void ExecuteHello(object parameter)
        {
            MessageBox.Show("Hello: " + parameter.ToString());
        }
    }
}

或者:-------------------

private DelegateCommand clickNewButton;

        public DelegateCommand ClickNewButton
        {
            get
            {
                if (clickNewButton == null)
                {
                    clickNewButton = new DelegateCommand((parameter) =>
                    {
                        parameter.GetType().GetMethod("Show").Invoke(parameter, null);
                    });
                }
                return clickNewButton;
            }
            set { clickNewButton = value; }
        }


 

方法二:-----------------------------------------------------------------

xmlns :Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

 

< Interactivity : Interaction.Behaviors >

< PopSearchAttributeEntityBehaviors : PopSearchAttributeEntityBehaviors >

< Interactivity : Interaction.Triggers >

< Interactivity : EventTrigger SourceName ="ButtonCancel" EventName ="Click">

< Interactivity : InvokeCommandAction CommandName ="CancelCommand" />

Interactivity : EventTrigger >

< Interactivity : EventTrigger SourceName ="ButtonOK" EventName ="Click">

< Interactivity : InvokeCommandAction CommandName ="OkClickedCommand" />

Interactivity : EventTrigger >

Interactivity : Interaction.Triggers >

PopSearchAttributeEntityBehaviors : PopSearchAttributeEntityBehaviors >

Interactivity : Interaction.Behaviors >

 

< Interactivity : Interaction.Triggers >

< Interactivity : EventTrigger SourceName ="DataGridSearchResult" EventName ="CheckBoxSelected">

< Interactivity : InvokeCommandAction Command ="{ Binding AttributesSelectedCommand }" />

Interactivity : EventTrigger >

Interactivity : Interaction.Triggers >

 


Behavior的Code部分:-----------------------------------------------

public class PopSearchAttributeEntityBehaviors : Behavior < ChildWindow >

{

private Collection < object > dataGridSelectRows;

public Collection < object > DataGridSelectRows

{

get { return dataGridSelectRows; }

set { dataGridSelectRows = value ; }

}

///

/// Close Menu Command

///

public ICommand CancelCommand { get ; set ; }

public ICommand OkClickedCommand { get ; set ; }

public PopSearchAttributeEntityBehaviors()

:

base ()

{

CancelCommand =

new DelegateCommand (OnCancel);

OkClickedCommand =

new DelegateCommand (OnOkClicked);

}

///

/// button Cancel

///

/// StackPanel Menu Object

public void OnCancel( object menu)

{

(

this .AssociatedObject as PopSearchAttributeEntity ).Close();

}

///

/// button ok click

///

///

public void OnOkClicked( object menu)

{

ObservableCollection < object > selectedItems = (( this .AssociatedObject as PopSearchAttributeEntity ).DataContext as PopSearchAttributeEntityViewModel ).SelectedAttributes;

((

this .AssociatedObject as PopSearchAttributeEntity ).DataContext as PopSearchAttributeEntityViewModel ).OkCallback(selectedItems);

(

this .AssociatedObject as PopSearchAttributeEntity ).Close();

}

}

你可能感兴趣的:(dev_SL_0)