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

///

<summary>

/// DelegateCommand

/// </summary>

public class DelegateCommand : ICommand

{

private Predicate < object > canExecute;

private Action < object > method;

/// <summary>

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

/// </summary>

public event EventHandler CanExecuteChanged;

/// <summary>

/// Initializes a new instance of the <see cref="DelegateCommand"/> class.

/// </summary>

/// <param name="method"> The method. </param>

public DelegateCommand( Action < object > method)

:

this (method, null )

{

}

/// <summary>

/// Initializes a new instance of the <see cref="DelegateCommand"/> class.

/// </summary>

/// <param name="method"> The method. </param>

/// <param name="canExecute"> The can execute. </param>

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

{

this .method = method;

this .canExecute = canExecute;

}

/// <summary>

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

/// </summary>

/// <param name="parameter"> Data used by the command. If the command does not require data to be passed, this object can be set to null. </param>

/// <returns>

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

/// </returns>

public bool CanExecute( object parameter)

{

if (canExecute == null )

{

return true ;

}

return canExecute(parameter);

}

/// <summary>

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

/// </summary>

/// <param name="parameter"> Data used by the command. If the command does not require data to be passed, this object can be set to null. </param>

public void Execute( object parameter)

{

method.Invoke(parameter);

}

/// <summary>

/// Raises the <see cref="E:CanExecuteChanged"/> event.

/// </summary>

/// <param name="e"> The <see cref="System.EventArgs"/> instance containing the event data. </param>

protected virtual void OnCanExecuteChanged( EventArgs e)

{

var canExecuteChanged = CanExecuteChanged;

if (canExecuteChanged != null )

canExecuteChanged(

this , e);

}

/// <summary>

/// Raises the can execute changed.

/// </summary>

public void RaiseCanExecuteChanged()

{

OnCanExecuteChanged(

EventArgs .Empty);

}

}


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

<navigation:Page x:Class="Silverlight40.Binding.Command.Demo" 
           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">
    <Grid x:Name="LayoutRoot">
        
        <Grid.DataContext>
            <ViewModel:MyViewModel />
        </Grid.DataContext>
        <!--
            Command - 指定需要关联的命令
            CommandParameter - 传递给 Command 的参数
        -->
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" Height="20">
            <TextBox Name="txtName" Text="webabcd" />
            <Button Content="Hello" Command="{Binding Hello}" CommandParameter="{Binding ElementName=txtName, Path=Text }" />
        </StackPanel>
        
    </Grid>
</navigation: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 ; }

}

/// <summary>

/// Close Menu Command

/// </summary>

public ICommand CancelCommand { get ; set ; }

public ICommand OkClickedCommand { get ; set ; }

public PopSearchAttributeEntityBehaviors()

:

base ()

{

CancelCommand =

new DelegateCommand (OnCancel);

OkClickedCommand =

new DelegateCommand (OnOkClicked);

}

/// <summary>

/// button Cancel

/// </summary>

/// <param name="state"> StackPanel Menu Object </param>

public void OnCancel( object menu)

{

(

this .AssociatedObject as PopSearchAttributeEntity ).Close();

}

/// <summary>

/// button ok click

/// </summary>

/// <param name="menu"></param>

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

icewizardry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值