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

AYUI 内置的Command的Behavior使用方法

时间:2016年10月24日 | 作者 : aaronyang | 分类 : AY梦 | 浏览: 2864次 | 评论 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,”的文章

猜你喜欢

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

发表评论

必填

选填

选填

必填

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

  查看权限

抖音:wpfui 工作wpf,目前主maui

招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰

目前在合肥市企迈科技就职

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

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

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

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

标签列表