当前位置:网站首页 / AY梦 / 正文

AYUI 内置的Command的Behavior使用方法

时间:2016年10月24日 | 作者 : aaronyang | 分类 : AY梦 | 浏览: 1321次 | 评论 0

DEMO 


前台


   

        <TextBlock Text="Attached Command Behaviors aka ACB" FontWeight="Bold" FontSize="16"/>

        <Border CornerRadius="5" Margin="10" BorderBrush="Silver" BorderThickness="1" Grid.Row="1" TextElement.FontSize="14">
            <StackPanel>
                <TextBlock Text="List of the last commands executed: " FontWeight="Bold"/>
                <ListBox ItemsSource="{Binding Messages}" Height="110" />
                <Button Content="Clear" Command="{Binding ClearMessagesCommand}"/>
            </StackPanel>
        </Border>

        <StackPanel Grid.Row="2" >

            <WrapPanel Margin="0,10,0,20">
                <TextBlock FontSize="14" Text="Border hooked to MouseDown event" Width="410"/>
                <Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2"
                        local:CommandBehavior.Event="MouseDown" 
                        local:CommandBehavior.Command="{Binding SomeCommand}"
                        local:CommandBehavior.CommandParameter="MouseDown"
                        >
                    <TextBlock Text="MouseDown on this border to execute the command"/>
                </Border>
            </WrapPanel>

            <WrapPanel Margin="0,0,0,20">
                <TextBlock FontSize="14" Text="Border hooked to the event selected in the ComboBox" Width="410"/>
                <Border Background="YellowGreen" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2"
                        local:CommandBehavior.Event="{Binding ElementName=events1, Path=SelectedItem}" 
                        local:CommandBehavior.Command="{Binding SomeCommand}"
                        local:CommandBehavior.CommandParameter="{Binding ElementName=events1, Path=SelectedItem}"
                        >
                    <WrapPanel>
                        <TextBlock Text="{Binding ElementName=events1, Path=SelectedItem}"/>
                        <TextBlock Text=" on this border to execute the command"/>
                    </WrapPanel>
                </Border>
                <ComboBox ItemsSource="{Binding Events}" MinWidth="200" SelectedIndex="1" x:Name="events1"/>
            </WrapPanel>

            <WrapPanel Margin="0,0,0,20">
                <TextBlock FontSize="14" Text="Border hooked to an Action instead of a Command" Width="410" />
                <Border Background="DarkSalmon" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" 
                        local:CommandBehavior.Event="MouseDown"  
                        local:CommandBehavior.Action="{Binding DoSomething}"
                        local:CommandBehavior.CommandParameter="from the DarkSalmon Border :P"/>
            </WrapPanel>
            
            <WrapPanel Margin="0,10,0,20">
               
                <TextBlock FontSize="14" Text="Border hooked to MouseDown event" Width="410"/>
                <Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test">
                    <local:CommandBehaviorCollection.Behaviors>
                            <local:BehaviorBinding Event="MouseLeftButtonDown" Action="{Binding DoSomething}" CommandParameter="An Action on MouseLeftButtonDown"/>
                            <local:BehaviorBinding Event="MouseRightButtonDown" Command="{Binding SomeCommand}" CommandParameter="A Command on MouseRightButtonDown"/>
                    </local:CommandBehaviorCollection.Behaviors>
                    <TextBlock Text="MouseDown on this border to execute the command"/>
                </Border>
            </WrapPanel>
        </StackPanel>

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.ComponentModel;
using System.Collections.ObjectModel;
using AttachedCommandBehavior;

namespace AttachedCommandBehaviorDemo
{
    public class DemoViewModel
    {
        /// <summary>
        /// Gets the list of events to bind to
        /// </summary>
        public IList<string> Events { get; private set; }

        /// <summary>
        /// Gets the list of Messages populated (The messages are the names of the events that execute the commands)
        /// </summary>
        public IList<string> Messages { get; private set; }

        /// <summary>
        /// Gets an action that adds a message
        /// </summary>
        public Action<object> DoSomething { get; private set; }

        /// <summary>
        /// Command that clears the list of messages
        /// </summary>
        public ICommand ClearMessagesCommand { get; private set; }

        /// <summary>
        /// Command that write the event name that executed the command
        /// </summary>
        public ICommand SomeCommand { get; private set; }

        public DemoViewModel()
        {
            DoSomething = x => Messages.Add("Action executed: " + x.ToString());
            Messages = new ObservableCollection<string>();
            Events = new[] 
            {
                "PreviewMouseDown",
                "PreviewMouseUp",
                "PreviewMouseLeftButtonDown",
                "PreviewMouseLeftButtonUp",
                "PreviewMouseRightButtonDown",
                "PreviewMouseRightButtonUp",
                "MouseEnter",
                "MouseLeave"
            };

            SomeCommand = new SimpleCommand
            {
                //this will set the Message property to the value of the CommandParameter
                ExecuteDelegate = x => Messages.Add(x.ToString())
            };
            ClearMessagesCommand = new SimpleCommand
            {
                ExecuteDelegate = x => Messages.Clear(),
                CanExecuteDelegate = x => Messages.Count > 0
            };
            DoSomething = x => Messages.Add("Action executed: " + x.ToString());
        }
    }
}


推荐您阅读更多有关于“AYUI,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

作者:杨洋(AaronYang简称AY,安徽六安人)目前是个人,还没成立公司AY唯一QQ:875556003和AY交流

高中学历,2015年1月17日开始,兴趣学习研究WPF

声明:AYUI个人与商用免费,源码可购买。部分DEMO不免费

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

兼容XP到win10,vs2015/2019,最新AYUI:7.6.4

vs2015 企业版密钥HM6NR-QXX7C-DFW2Y-8B82K-WTYJV

vs2017 企业版密钥NJVYC-BMHX2-G77MM-4XJMR-6Q8QF

标签列表