Intersoft ClientUI 8 > ClientUI Fundamentals > Commanding Overview > Commanding How-to Topics > How-to: Explicitly Invalidate the State of a Command Source |
This example shows how to explicitly invalidate the state of a command source.
The CommandManager only pays attention to certain conditions in determining when the command target has changed, such as change in keyboard focus. In situations where the CommandManager does not sufficiently determine a change in conditions that cause a command to not be able to execute, InvalidateRequerySuggested can be called to force the CommandManager to raise the RequerySuggested event.
The following example shows how to invalidate the state of a command source by calling the InvalidateRequerySuggested method to requery the Copy command when the selection of the text editor has changed.
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage 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" mc:Ignorable="d" xmlns:Intersoft="http://intersoft.clientui.com/schemas" xmlns:Commands="clr-namespace:ClientUIApplication_Docs.Commands" x:Class="ClientUIApplication_Docs.RoutedCommands.InvalidateRequery" Title="InvalidateRequery Page" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot"> <Intersoft:CommandManager.CommandBindings> <Intersoft:CommandBindingCollection> <Intersoft:CommandBinding Command="Commands:EditingCommands.Copy" CanExecute="CopyCmdCanExecute" Executed="CopyExecuted"/> </Intersoft:CommandBindingCollection> </Intersoft:CommandManager.CommandBindings> <Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom"> <Intersoft:UXToolBar Name="toolBar1" Intersoft:DockPanel.Dock="Top"> <Intersoft:UXToolGroup> <Intersoft:UXToolBarButton Name="btn_Cut" Command="Commands:EditingCommands.Cut" DisplayMode="Image" Icon="../Images/CutHS.png"/> <Intersoft:UXToolBarButton Name="btn_Copy" Command="Commands:EditingCommands.Copy" DisplayMode="Image" Icon="../Images/CopyHS.png"/> <Intersoft:UXToolBarButton Name="btn_Paste" Command="Commands:EditingCommands.Paste" DisplayMode="Image" Icon="../Images/PasteHS.png"/> </Intersoft:UXToolGroup> </Intersoft:UXToolBar> <Intersoft:UXTextBox Name="textBox1" Text="Type your text here..." Intersoft:DockPanel.IsFillElement="True" /> </Intersoft:DockPanel> </Grid> </Intersoft:UXPage> |
C# |
Copy Code
|
---|---|
using System.Windows; using ClientUIApplication_Docs.Commands; using Intersoft.Client.Framework.Input; using Intersoft.Client.UI.Navigation; namespace ClientUIApplication_Docs.RoutedCommands { public partial class InvalidateRequery : UXPage { public InvalidateRequery() { EditingCommands.Initialize(); InitializeComponent(); textBox1.SelectionChanged += new RoutedEventHandler(textBox1_SelectionChanged); } private void CopyCmdCanExecute(object sender, CanExecuteRoutedEventArgs e) { // When the textbox has selection, the Copy command will be enabled. // Otherwise, it will be disabled. e.CanExecute = (textBox1.SelectionLength > 0); } private void CopyExecuted(object sender, ExecutedRoutedEventArgs e) { string command = ((RoutedCommand)e.Command).Name; string target = ((FrameworkElement)e.OriginalSource).Name; MessageBox.Show("The " + command + " command has been invoked on target object " + target); } private void textBox1_SelectionChanged(object sender, RoutedEventArgs e) { // Invalidate the commands in this page, // this will call the CanExecute event handler of each commands // to update their CanExecute state. CommandManager.InvalidateRequerySuggested(); } } } |
With the above implementation, the Copy button in the tool bar will be initially disabled because the text box does not have selection. When you select a word in the text box, notice that the Copy button will now be enabled. Similarly, it will be disabled again when you clear the selection of the text box.
The InvalidateRequerySuggested function, when properly used along with other commanding concepts, enable you to create rich user experiences that work consistently and reliably. To learn more about commanding, see Commanding Overview.
To learn how to create the routed commands for EditingCommands used in the above example, see How-to: Create Routed Commands In a Static Class.