【WPF学习】第三十三章 高级命令

  前面两章介绍了命令的基本内容,可考虑一些更复杂的实现了。接下来介绍如何使用本身的命令,根据目标以不一样方式处理相同的命令以及使用命令参数,还将讨论如何支持基本的撤销特性。数据结构

1、自定义命令app

  在5个命令类(ApplicationCommands、NavigationCommands、EditingCommands、ComponentCommands以及MediaCommands)中存储的命令,显然不会为应用程序提供全部可能须要的命令。幸运的是,能够很方便地自定义命令,须要作的所有工做就是实例化一个新的RoutedUiCommand对象。编辑器

  RoutedUICommand类提供了几个构造函数。虽然可建立没有任何附加信息的RoutedUICommand对象,但几乎老是但愿提供命令名、命令文本以及所属类型。此外,可能但愿为InputGestures集合提供快捷键。ide

  最佳设计方式是遵循WPF库中的范例,并经过静态属性提供自定义命令。下面的示例定义了名为Requery的命令:函数

 public class DataCommands
    {
        private static RoutedUICommand requery;
        static DataCommands()
        {
            InputGestureCollection collection = new InputGestureCollection();
            collection.Add(new KeyGesture(Key.R, ModifierKeys.Control, "Ctrl+R"));
            requery = new RoutedUICommand("Requery", "Requery", typeof(DataCommands), collection);
        }

        public static RoutedUICommand Requery
        {
            get { return requery; }
            set { requery = value; }
        }
    }

  一旦定义了命令,就能够在命令绑定中使用它,就像使用WPF提供的全部预先构建好的命令那样。但仍存在一个问题。若是但愿在XAML中使用自定义的命令,那么首先须要将.NET名称空间映射为XML名称空间。例如,若是自定义的命令类位于Commands名称空间中(对于名为Commands的项目,这是默认的名称空间),那么应添加以下名称空间映射:工具

xmlns:local="clr-namespace:Commands"

  这个示例使用local做为名称空间的别名。也可以使用任意但愿使用的别名,只要在XAML文件中保持一致就能够了。学习

  如今,可经过local名称空间访问命令:this

<CommandBinding Command="local:DataCommands.Requery" 
                Executed="CommandBinding_Executed">
</CommandBinding>

  下面是一个完整示例,在该例中有一个简单的窗口,该窗口包含一个触发Requery命令的按钮:spa

<Window x:Class="Commands.CustomCommand"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Commands"
        Title="CustomCommand" Height="300" Width="300">
    <Window.CommandBindings>
        <CommandBinding Command="local:DataCommands.Requery" 
                        Executed="CommandBinding_Executed">
        </CommandBinding>
    </Window.CommandBindings>
    <Grid>
        <Button Margin="5" Command="local:DataCommands.Requery">Requery</Button>
    </Grid>
</Window>

  为完成该例,只须要在代码中实现CommandBinding_Executed()事件处理程序便可。还可使用CanExecute事件酌情启用或禁用该命令。设计

2、在不一样位置使用相同的命令

  在WPF命令模型中,一个重要概念是范围(scope)。尽管每一个命令仅有一份副本,但使用命令的效果却会根据触发命令的位置而异。例如,若是有两个文本框,它们都支持Cut、Copy和Paste命令,操做只会在当前具备焦点的文本框中发生。

  至此,咱们尚未学习如何对本身关联的命令实现这种效果。例如,设想建立了一个具备两个文档的控件的窗口,以下图所示。

 

   若是使用Cut、Copy和Paste命令,就会发现他们可以在正确的文本框中自动工做。然而,对于本身实现的命令——New、Open以及Save命令——状况就不一样了。问题在于当为这些命令中的某个命令触发Executed事件时,不知道该事件是属于第一个文本框仍是第二个文本框。尽管ExecuteRoutedEventArgs对象提供了Source属性,但该属性反映的是具备命令绑定的元素(像sender引用)。而到目前为止,全部命令都被绑定到了容器窗口。

  解决这个问题的方法是使用文本框的CommandBindings集合分别为每一个文本框绑定命令。下面是一个示例:

<TextBox Margin="5" Grid.Row="3" TextWrapping="Wrap" AcceptsReturn="True"
             TextChanged="txt_TextChanged">
            <TextBox.CommandBindings>
                <CommandBinding Command="ApplicationCommands.Save"
          Executed="SaveCommand" />
            </TextBox.CommandBindings>
</TextBox>

  如今文本框处理Executed事件。在事件处理程序中,可以使用这一信息确保保存正确的信息:

private void SaveCommand(object sender, ExecutedRoutedEventArgs e)
        {
            string text = ((TextBox)sender).Text;
            MessageBox.Show("About to save: " + text);
            isDirty= false;
        }

  上面的实现存在两个小问题。首先,简单的isDirty标记不在能知足须要,所以如今须要跟踪两个文本框。有几种解决这个问题的方法。可以使用TextBox.Tag属性存储isDirty标志——使用该方法,不管什么时候调用CanExecuteSave()方法,均可以查看sender的Tag属性。也可建立私有的字典集合来保存isDirty值,按照控件引用编写索引。当触发CanExecuteSave()方法时,查找属于sender的isDirty值。下面是须要使用的完整代码:

 private Dictionary<Object, bool> isDirty = new Dictionary<Object, bool>();
        private void txt_TextChanged(object sender, RoutedEventArgs e)
        {
            isDirty[sender] = true;
        }

        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (isDirty.ContainsKey(sender) && isDirty[sender] == true)
            {
                e.CanExecute = true;
            }
            else
            {
                e.CanExecute = false;
            }
        }

  当前实现的另外一个问题是建立了两个命令绑定,而实际上只须要一个。这会是XAML文件更加混乱,维护起来更难。若是在这两个文本框之间又大量的共享的命令,这个问题尤为明显。

  解决方法是建立命令绑定,并向两个文本框的CommandBindings集合中添加同一个绑定。使用代码可很容易地完成该工做。若是但愿使用XAML,须要使用WPF资源。在窗口的顶部添加一小部分标记,建立须要使用的Command Binding对象,并为之指定键名:

<Window.Resources>
        <CommandBinding  x:Key="binding" Command="ApplicationCommands.Save"
                         Executed="SaveCommand" CanExecute="SaveCommand_CanExecute">
        </CommandBinding>
    </Window.Resources>

  为在标记的另外一个位置插入该对象,可以使用StaticResource标记扩展并提供键名:

<TextBox.CommandBindings>
     <StaticResource ResourceKey="binding"></StaticResource>
</TextBox.CommandBindings>

  该示例的完整代码以下所示:

<Window x:Class="Commands.TwoDocument"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TwoDocument" Height="300" Width="300">
    <Window.Resources>
        <CommandBinding  x:Key="binding" Command="ApplicationCommands.Save"
                         Executed="SaveCommand" CanExecute="SaveCommand_CanExecute">
        </CommandBinding>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition ></RowDefinition>
            <RowDefinition ></RowDefinition>
        </Grid.RowDefinitions>
        <Menu Grid.Row="0">
            <MenuItem Header="File">
                <MenuItem Command="New"></MenuItem>
                <MenuItem Command="Open"></MenuItem>
                <MenuItem Command="Save"></MenuItem>
                <MenuItem Command="SaveAs"></MenuItem>
                <Separator></Separator>
                <MenuItem Command="Close"></MenuItem>
            </MenuItem>
        </Menu>

        <ToolBarTray Grid.Row="1">
            <ToolBar>
                <Button Command="New">New</Button>
                <Button Command="Open">Open</Button>
                <Button Command="Save">Save</Button>
            </ToolBar>
            <ToolBar>
                <Button Command="Cut">Cut</Button>
                <Button Command="Copy">Copy</Button>
                <Button Command="Paste">Paste</Button>
            </ToolBar>
        </ToolBarTray>
        <TextBox Margin="5" Grid.Row="2" TextWrapping="Wrap" AcceptsReturn="True"
             TextChanged="txt_TextChanged">
            <TextBox.CommandBindings>
                <StaticResource ResourceKey="binding"></StaticResource>
            </TextBox.CommandBindings>
            <!--<TextBox.CommandBindings>
                <CommandBinding Command="ApplicationCommands.Save"
          Executed="SaveCommand" />
            </TextBox.CommandBindings>-->
        </TextBox>
        <TextBox Margin="5" Grid.Row="3" TextWrapping="Wrap" AcceptsReturn="True"
             TextChanged="txt_TextChanged">
            <TextBox.CommandBindings>
                <StaticResource ResourceKey="binding"/>
            </TextBox.CommandBindings>
            <!--<TextBox.CommandBindings>
                <CommandBinding Command="ApplicationCommands.Save"
          Executed="SaveCommand" />
            </TextBox.CommandBindings>-->
        </TextBox>
    </Grid>
</Window>
TwoDocument.xaml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Commands
{
    /// <summary>
    /// TwoDocument.xaml 的交互逻辑
    /// </summary>
    public partial class TwoDocument : Window
    {
        public TwoDocument()
        {
            InitializeComponent();
        }


        private void SaveCommand(object sender, ExecutedRoutedEventArgs e)
        {
            string text = ((TextBox)sender).Text;
            MessageBox.Show("About to save: " + text);
            isDirty[sender] = false;
        }

        private Dictionary<Object, bool> isDirty = new Dictionary<Object, bool>();
        private void txt_TextChanged(object sender, RoutedEventArgs e)
        {
            isDirty[sender] = true;
        }

        private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (isDirty.ContainsKey(sender) && isDirty[sender] == true)
            {
                e.CanExecute = true;
            }
            else
            {
                e.CanExecute = false;
            }
        }
    }
}
TwoDocument.xaml.cs

3、使用命令参数

  上面全部的示例都没有使用命令参数来传递额外信息。然而,有些命令总须要一些额外信息。例如,NavigationCommands.Zoom命令须要用于缩放的百分数。相似地,可设想在特定状况下,前面使用过的一些命令可能也须要额外信息。例如,上节示例所示的两个文本框编辑器使用Save命令,当保存文档时须要知道使用哪一个文件。

  解决方法是设置CommandParameter属性。可直接为ICommandSource控件设置该属性(甚至可以使用绑定表达式从其余控件获取值)。例如,下面的代码演示了如何经过从另外一个文本框中读取数值,为连接到Zoom命令的按钮设置缩放百分比:

<Button Command="NavigationCommands.Zoom"
    CommandParater="{Binding ElementName=txtZoom,Path=Text"}>
Zoom To Value
</Button>

  但该方法并不老是有效。例如,在具备两个文件的文本编辑器中,每一个文本框重用同一个Save按钮,但每一个文本框须要使用不一样的文件名。对于此类状况,必须在其余地方存储信息(例如,在TextBox.Tag属性或在为区分文本框而索引文件名称的单独集合中存储信息),或者须要经过代码触发命令,以下所示:

ApplicationCommands.New.Execute(theFileName,(Button)sender);

  不管使用哪一种方法,均可以在Executed事件处理程序中经过ExecutedRoutedEventArgs.Parameter属性获取参数。

4、跟踪和翻转命令

  WPF命令模型缺乏的一个特性是翻转命令。尽管提供了ApplicationCommands.Undo命令,但该命令一般用于编辑控件(如TextBox控件)以维护它们本身的Undo历史。若是但愿支持应用程序范围内的Undo特性,须要在内部跟踪之前的状态,而且触发Undo命令时还原该状态。

  遗憾的是,扩展WPF命令系统并不容易。相对来讲没几个入口点用于链接自定义逻辑,而且对于可用的几个入口点也没有提供说明文档。为建立通用的、可重用的Undo特性,须要建立一组全新的“可以撤销的”命令类,以及一个特定类型的命令绑定。本质上,必须使用本身建立的新命令系统替换WPF命令系统。

  更好的解决方案是设计本身的用于跟踪和翻转命令的系统,但使用CommandManager类保存命令历史。下图显示了一个这方面的例子。在该例中,窗口包含两个文本框和一个列表框,能够自由地再这两个文本框中输入内容,而列表框则一直跟踪在这两个文本框中发生的全部命令。可经过单击Reverse Last Command按钮翻转最后一个命令。

 

   为构建这个解决方案,须要使用几项新技术。第一细节是用于跟踪命令历史的类。为构建保存最近命令的撤销系统,肯恩共须要用到这样的类(甚至可能喜欢建立派生的ReversibleCommand类,提供诸如Unexecute()的方法来翻转之前的任务)。但该系统不能工做,由于全部WPF命令都是惟一的。这意味着在应用程序中每一个命令只有一个实例。

  为理解该问题,假设提供EditingCommands.Backspace命令,并且用户在一行中回退了几个空格。可经过向最近命令堆栈中添加Backspace命令来记录这一操做,但实际上每次添加的是相同的命令对象。所以,没有简单的方法用于存储命令的其余信息,例如刚刚删除的字符。若是但愿存储该状态,须要构建本身的数据结构。该例使用名为CommandHistoryItem的类。

  每一个CommandHistoryItem对象跟踪如下几部分信息:

  •   命令名称
  •   执行命令的元素。在该例中,有两个文本框,因此能够是其中的任意一个。
  •   在目标元素中被改变的属性。在该例中是TextBox类的Text属性。
  •   可用于保存受影响元素之前状态的对象(例如,执行命令以前文本框中的文本)。

  CommandHistoryItem类还提供了通用的Undo()方法。该方法使用反射为修改过的属性应用之前的值,用于恢复TextBox控件中的文本。但对于更复杂的应用程序,须要使用CommandHistoryItem类的层次结构,每一个类均可以使用不一样方式翻转不一样类型的操做。

  下面是CommandHistoryItem类的完整代码。

public class CommandHistoryItem
    {
        public string CommandName
        {
            get;
            set;
        }

        public UIElement ElementActedOn
        {
            get;
            set;
        }

        public string PropertyActedOn
        {
            get;
            set;
        }

        public object PreviousState
        {
            get;
            set;
        }

        public CommandHistoryItem(string commandName)
            : this(commandName, null, "", null)
        { }

        public CommandHistoryItem(string commandName, UIElement elementActedOn,
            string propertyActedOn, object previousState)
        {
            CommandName = commandName;
            ElementActedOn = elementActedOn;
            PropertyActedOn = propertyActedOn;
            PreviousState = previousState;
        }
        public bool CanUndo
        {
            get { return (ElementActedOn != null && PropertyActedOn != ""); }
        }

        public void Undo()
        {
            Type elementType = ElementActedOn.GetType();
            PropertyInfo property = elementType.GetProperty(PropertyActedOn);
            property.SetValue(ElementActedOn, PreviousState, null);
        }
    }

  须要的下一个要素是执行应用程序范围内Undo操做的命令。ApplicationCommands.Undo命令时不适合的,缘由是为了达到不一样的目的,它已经被用于单独的文本框控件(翻转最后的编辑变化)。相反,须要建立一个新命令,以下所示:

private static RoutedUICommand applicationUndo;
        public static RoutedUICommand ApplicationUndo
        {
            get { return applicationUndo; }
        }

        static MonitorCommands()
        {
            applicationUndo = new RoutedUICommand("ApplicationUndo", "Application Undo", typeof(MonitorCommands));
            
        }

  在该例中,命令时在名为MonitorCommands的窗口类中定义的。

  到目前为止,出了执行Undo操做的反射代码比较有意义外,其余代码没有什么值得注意的地方。更困难的部分是将该命令历史集成进WPF命令模型中。理想的解决方案是使用能跟踪任意命令的方式完成该任务,而无论命令是是被如何触发和绑定的。相对不理想的解决方案是,强制依赖与一整套全新的自定义命令对象(这一逻辑功能内置到这些自定义命令对象中),或手动处理每一个命令的Executed事件。

  响应特定的命令是很是简单的,但当执行任何命令时如何进行响应呢?技巧是使用CommandManager类,该类提供了几个静态事件。这些事件包括CanExecute、PreviewCanExecute、Executed以及PreviewExecuted。在该例中,Executed和PreviewExecuted事件最有趣,由于每当执行任何一个命令时都会引起他们。

  尽管CommandManager类关起了Executed事件,但仍可以使用UIElement.AddHandler()方法关联事件处理程序,并为可选的第三个参数传递true值。这样将容许接收事件,即便事件已经被处理过也一样如此。然而,Executed事件是在命令执行完以后被触发的,这时已经来不及在命令历史中保存呗影响的控件的状态了。相反,须要响应PreviewExecuted事件,该事件在命令执行前一刻被触发。

  下面的代码在窗口的构造函数中关联PreviewExecuted事件处理程序,并当关闭窗口时解除关联:

 public MonitorCommands()
        {
            InitializeComponent();
            this.AddHandler(CommandManager.PreviewExecutedEvent,
               new ExecutedRoutedEventHandler(CommandExecuted));
        }

        private void window_Unloaded(object sender, RoutedEventArgs e)
        {
            this.RemoveHandler(CommandManager.PreviewExecutedEvent,
               new ExecutedRoutedEventHandler(CommandExecuted));
        }

  当触发PreviewExecuted事件时,须要肯定准备执行的命令是不是咱们所关心的。若是是,可建立CommandHistoryItem对象,并将其添加到Undo堆栈中。还须要注意两个潜在的问题。第一个问题是,当单击工具栏按钮以在文本框上执行命令时,CommandExecuted事件被引起了两次——一次是针对工具栏按钮,另外一次时针对文本框。下面的代码经过忽略发送者是ICommandSource的命令,避免在Undo历史中重复条目。第二个问题是,须要明确忽略不但愿添加到Undo历史中的命令。例如ApplicationUndo命令,经过该命令可翻转上一步操做。

private void CommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            // Ignore menu button source.
            if (e.Source is ICommandSource) return;

            // Ignore the ApplicationUndo command.
            if (e.Command == MonitorCommands.ApplicationUndo) return;

            // Could filter for commands you want to add to the stack
            // (for example, not selection events).

            TextBox txt = e.Source as TextBox;
            if (txt != null)
            {
                RoutedCommand cmd = (RoutedCommand)e.Command;

                CommandHistoryItem historyItem = new CommandHistoryItem(
                    cmd.Name, txt, "Text", txt.Text);

                ListBoxItem item = new ListBoxItem();
                item.Content = historyItem;
                lstHistory.Items.Add(historyItem);

                // CommandManager.InvalidateRequerySuggested();
            }
        }

  该例在ListBox控件中存储全部CommandHistoryItem对象。ListBox控件的DisplayMember属性被设置为true,于是会显示每一个条目的CommandHistoryItem.Name属性。上面的代码只为由文本框引起的命令提供Undo特性。然而,处理窗口中的任何文本框一般就足够了。为了支持其余控件和属性,须要对代码进行扩展。

  最后一个细节是直线应用程序中范围内Undo操做的代码。使用CanExecute事件处理程序,可确保只有当在Undo历史中至少有一项时,才能执行此代码:

 private void ApplicationUndoCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (lstHistory == null || lstHistory.Items.Count == 0)
                e.CanExecute = false;
            else
                e.CanExecute = true;
        }

  为恢复最近的修改,只须要调用CommandHistoryItem对象的Undo方法。而后从列表中删除该项便可:

private void ApplicationUndoCommand_Executed(object sender, RoutedEventArgs e)
        {
            CommandHistoryItem historyItem = (CommandHistoryItem)lstHistory.Items[lstHistory.Items.Count - 1];
            if (historyItem.CanUndo) historyItem.Undo();
            lstHistory.Items.Remove(historyItem);
        }

  到此,该示例的全部涉及细节都已经处理完成,该应用程序具备几个彻底支持Undo特性的控件,但要在实际应用程序中使用这一方法,还须要进行许多改进。例如,须要耗费大量时间改进CommandManager.PreviewExecuted事件的处理程序,以忽略那些明星不须要跟踪的命令(当前,诸如使用键盘选择文本的事件已经单击空格键引起的命令等)。相似地,可能但愿为那些不是由命令表示的但应当被翻转的操做添加CommandHistoryItem对象。例如,输入一些文本,而后导航到其余控件等。

  本实例完整代码以下所示:

<Window x:Class="Commands.MonitorCommands"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Commands"
        Title="MonitorCommands" Height="300" Width="329.323" Unloaded="window_Unloaded">
    <Window.CommandBindings>

        <CommandBinding Command="local:MonitorCommands.ApplicationUndo"
                    Executed="ApplicationUndoCommand_Executed"
                    CanExecute="ApplicationUndoCommand_CanExecute"></CommandBinding>
    </Window.CommandBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>


        <ToolBarTray  Grid.Row="0">
            <ToolBar>
                <Button Command="ApplicationCommands.Cut">Cut</Button>
                <Button Command="ApplicationCommands.Copy">Copy</Button>
                <Button Command="ApplicationCommands.Paste">Paste</Button>
                <Button Command="ApplicationCommands.Undo">Undo</Button>
            </ToolBar>
            <ToolBar Margin="0,0,-23,0">
                <Button Command="local:MonitorCommands.ApplicationUndo">Reverse Last Command</Button>
            </ToolBar>
        </ToolBarTray>
        <TextBox Margin="5" Grid.Row="1"
             TextWrapping="Wrap" AcceptsReturn="True">
        </TextBox>
        <TextBox Margin="5" Grid.Row="2"
             TextWrapping="Wrap" AcceptsReturn="True">
        </TextBox>
        <ListBox Grid.Row="3" Name="lstHistory" Margin="5" DisplayMemberPath="CommandName"></ListBox>
    </Grid>
</Window>
MonitorCommands.xaml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Commands
{
    /// <summary>
    /// MonitorCommands.xaml 的交互逻辑
    /// </summary>
    public partial class MonitorCommands : Window
    {
        private static RoutedUICommand applicationUndo;
        public static RoutedUICommand ApplicationUndo
        {
            get { return applicationUndo; }
        }

        static MonitorCommands()
        {
            applicationUndo = new RoutedUICommand("ApplicationUndo", "Application Undo", typeof(MonitorCommands));
            
        }
        public MonitorCommands()
        {
            InitializeComponent();
            this.AddHandler(CommandManager.PreviewExecutedEvent,
               new ExecutedRoutedEventHandler(CommandExecuted));
        }

        private void window_Unloaded(object sender, RoutedEventArgs e)
        {
            this.RemoveHandler(CommandManager.PreviewExecutedEvent,
               new ExecutedRoutedEventHandler(CommandExecuted));
        }

        private void CommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            // Ignore menu button source.
            if (e.Source is ICommandSource) return;

            // Ignore the ApplicationUndo command.
            if (e.Command == MonitorCommands.ApplicationUndo) return;

            // Could filter for commands you want to add to the stack
            // (for example, not selection events).

            TextBox txt = e.Source as TextBox;
            if (txt != null)
            {
                RoutedCommand cmd = (RoutedCommand)e.Command;

                CommandHistoryItem historyItem = new CommandHistoryItem(
                    cmd.Name, txt, "Text", txt.Text);

                ListBoxItem item = new ListBoxItem();
                item.Content = historyItem;
                lstHistory.Items.Add(historyItem);

                // CommandManager.InvalidateRequerySuggested();
            }
        }

        private void ApplicationUndoCommand_Executed(object sender, RoutedEventArgs e)
        {
            CommandHistoryItem historyItem = (CommandHistoryItem)lstHistory.Items[lstHistory.Items.Count - 1];
            if (historyItem.CanUndo) historyItem.Undo();
            lstHistory.Items.Remove(historyItem);
        }

        private void ApplicationUndoCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (lstHistory == null || lstHistory.Items.Count == 0)
                e.CanExecute = false;
            else
                e.CanExecute = true;
        }
    }

    public class CommandHistoryItem
    {
        public string CommandName
        {
            get;
            set;
        }

        public UIElement ElementActedOn
        {
            get;
            set;
        }

        public string PropertyActedOn
        {
            get;
            set;
        }

        public object PreviousState
        {
            get;
            set;
        }

        public CommandHistoryItem(string commandName)
            : this(commandName, null, "", null)
        { }

        public CommandHistoryItem(string commandName, UIElement elementActedOn,
            string propertyActedOn, object previousState)
        {
            CommandName = commandName;
            ElementActedOn = elementActedOn;
            PropertyActedOn = propertyActedOn;
            PreviousState = previousState;
        }
        public bool CanUndo
        {
            get { return (ElementActedOn != null && PropertyActedOn != ""); }
        }

        public void Undo()
        {
            Type elementType = ElementActedOn.GetType();
            PropertyInfo property = elementType.GetProperty(PropertyActedOn);
            property.SetValue(ElementActedOn, PreviousState, null);
        }
    }
}
MonitorCommands.xaml.cs
相关文章
相关标签/搜索