码迷,mamicode.com
首页 > 其他好文 > 详细

Command自定义二

时间:2016-03-29 19:19:44      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

MyCommand.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace DefineCommand
{
    public class MyCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            throw new NotImplementedException();
        }

        public void Execute(object parameter)
        {
            TextBox txtCmd = parameter as TextBox;
            if (txtCmd != null)
            {
                MessageBox.Show(txtCmd.Text);
            }
        }
    }
}

 

 

MyCommandSource.cs

using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;

namespace DefineCommand
{
    class MyCommandSource : TextBlock, ICommandSource
    {
        public ICommand Command { get; set; }
        public object CommandParameter { get; set; }
        public IInputElement CommandTarget { get; set; }

        //重写单击处理函数,注意由于事件的优先级不同,如果命令源是Button的话,下面的函数不起作用
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            if (this.CommandTarget != null)
            {
                this.Command.Execute(this.CommandTarget);
            }
        }
    }
}

 

 

MainWindow.xaml

<Window x:Class="DefineCommand.MainWindow"
        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:local="clr-namespace:DefineCommand"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Border CornerRadius="5" BorderBrush="LightBlue" BorderThickness="2">
         <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <TextBox Text="自定义命令测试,请输入内容:" TextAlignment="Center" VerticalAlignment="Center" FontSize="18"/>
            <TextBox x:Name="myTxt" Height="40" Margin="18" Grid.Column="1"/>
            <local:MyCommandSource x:Name="mySource" Grid.Row="1" Grid.ColumnSpan="2"
                                   Text="单击此色块自定义命令测试" FontSize="23" TextAlignment="Center"
                                   VerticalAlignment="Center" Height="110" Width="410" Background="LightBlue"/>
        </Grid>
    </Border>
</Window>

 

 

MainWindow.xaml.cs

using System.Windows;

namespace DefineCommand
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyCommand myCmd = new MyCommand();
            this.mySource.Command = myCmd;
            this.mySource.CommandTarget = this.myTxt;
        }
    }
}

Command自定义二

标签:

原文地址:http://www.cnblogs.com/guojiangze/p/5333941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!