Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 32967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:50:48+00:00 2026-05-10T13:50:48+00:00

I’ve done some WPF programing and one thing I never got was the command

  • 0

I’ve done some WPF programing and one thing I never got was the command pattern. Every example seems to be for built in ones, edit, cut, paste. Anyone have an example or suggestion of best practice for custom commands?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:50:48+00:00Added an answer on May 10, 2026 at 1:50 pm

    Ah ha! A question I can answer! Firstly, I should mention that I have personally found it easier to define and hook up commands in code rather than in XAML. It allows me to hook up the handlers for the commands a little more flexibly than an all XAML approach does.

    You should work out what commands you want to have and what they relate to. In my application, I currently have a class for defining important application commands like so:

    public static class CommandBank {   /// Command definition for Closing a window   public static RoutedUICommand CloseWindow { get; private set; }    /// Static private constructor, sets up all application wide commands.   static CommandBank()   {     CloseWindow = new RoutedUICommand();     CloseWindow.InputGestures.Add(new KeyGesture(Key.F4, ModifierKeys.Alt));     // ...   } 

    Now, because I wanted to keep the code all together, using a code only approach to Commands lets me put the following methods in the class above:

    /// Closes the window provided as a parameter public static void CloseWindowExecute(object sender, ExecutedRoutedEventArgs e) {   ((Window)e.Parameter).Close(); }  /// Allows a Command to execute if the CommandParameter is not a null value public static void CanExecuteIfParameterIsNotNull(object sender, CanExecuteRoutedEventArgs e) {   e.CanExecute = e.Parameter != null;   e.Handled = true; } 

    The second method there can even be shared with other Commands without me having to repeat it all over the place.

    Once you have defined the commands like this, you can add them to any piece of UI. In the following, once the Window has Loaded, I add command bindings to both the Window and MenuItem and then add an input binding to the Window using a loop to do this for all command bindings. The parameter that is passed is the Window its self so the code above knows what Window to try and close.

    public partial class SimpleWindow : Window {   private void WindowLoaded(object sender, RoutedEventArgs e)   {     // ...     this.CommandBindings.Add(       new CommandBinding(         CommandBank.CloseWindow,         CommandBank.CloseWindowExecute,         CommandBank.CanExecuteIfParameterIsNotNull));      foreach (CommandBinding binding in this.CommandBindings)     {        RoutedCommand command = (RoutedCommand)binding.Command;        if (command.InputGestures.Count > 0)        {          foreach (InputGesture gesture in command.InputGestures)          {            var iBind = new InputBinding(command, gesture);            iBind.CommandParameter = this;            this.InputBindings.Add(iBind);          }        }     }      // menuItemExit is defined in XAML     menuItemExit.Command = CommandBank.CloseWindow;     menuItemExit.CommandParameter = this;     // ...   }    // .... } 

    I then also later have event handlers for the WindowClosing and WindowClosed events, I do recommend you make the actual implementation of commands as small and generic as possible. As in this case, I didn’t try to put code that tries to stop the Window closing if there is unsaved data, I kept that code firmly inside the WindowClosing event.

    Let me know if you have any follow up questions. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.