routed-commands

WPF Routed Events Across Element Tree Branches

I am wondering what the correct mechanism to enable communication between controls in WPF is. My goal is to not use conventional events and have to manually wire them up. The default behavior of routed commands (tunneling, bubbling) seems to be along the right lines but I guess I'm missing something. Routed events are a new infrastr...

What is the best approach to binding commands in a ViewModel to elements in the View?

Anyone who has tried to implement RoutedCommands in WPF using M-V-VM has undoubtedly run into issues. Commands (non-UI commands that is) should be implemented in the ViewModel. For instance if I needed to save a CustomerViewModel then I would implement that as a command directly on my CustomerViewModel. However if I wanted to pop up a wi...

Do WPF Routed Commands solve a problem or make it worse?

From what I understand, the goal of the Command pattern is to help separate UI interaction from application logic. With properly implemented commands, a click on a "Print" menu item might result in a chain of interaction like this: (button) ---click executes command----> (command) ---calls Print() in app logic ---> (logic) This encou...

How do I pass a specific viewmodel object in a button's CommandParam?

I have a simple WPF program using the Master-Detail UI pattern, where the Detail shows the currently selected item of a collection in the Master pane. I'm using MVVM, and each XAML page is backed by a ViewModel object which is set as it's DataContext. Now, I want to add a DELETE button in the Master pane to delete from the master list ...

contextsensitive RoutedUICommand.CanExecute, Execute

I have a single RoutedUICommand that can be accessed through various places in the UI. Such as global Keyboardshortcut, Menu, ContextMenu or Button. The code that is to be executed in the RoutedUICommand.CanExecute and RoutedUICommand.Execute methods depends on what UI element was used. How can I achieve this differentiation. I was thi...

WPF using custom RoutedUICommands or simple event handlers?

I was talking to someone today about picking a design pattern for how to handle logic in their WPF program and hoping that the SO community can help with further advice to make the decision easier. What factors in favour of commands outweigh the inconvenience? I prepared a full sample along with some UML diagrams of the first two of thr...

Expose multiple command in WPF user control

A better explanation, I hope: I have a toolbar with 3 buttons on it, all three bound to a Command (including a CommandParameter) this toolbar is used on several screens the xaml of the toolbar is exactly the same over all those screens I want to remove the toolbar instance and replace it with a user control that provides 3 commands, ...

Opening a new Window with InputGestures: main window steals focus

I have a simple UserControl which executes a routed command when user double clicks: <UserControl.InputBindings> <MouseBinding Command="someRoutedCommand" Gesture="LeftDoubleClick" /> </UserControl.InputBindings> Somewhere upper in the visual tree this routed command is bound to the procedure: <UserControl.CommandBindings> <C...

How to move my RoutedCommand handler from View-codebehind to ViewModel?

The following RoutedCommand example works. However, the handling for the button which executes the command is in the codebehind of the view. The way I understand MVVM, it should be in the ViewModel. However, When I move the method to the ViewModel (and change it to public), I get the error "ManagedCustomersView does not contain a defin...

WPF - Why do ContextMenu items work for ListBox but not ItemsControl?

Items in a list have context menus. The context menu items are bound to routed commands. The context menu items work correctly if the list control is a ListBox, but as soon as I downgrade it to an ItemsControl it no longer works. Specifically the menu items are always greyed out. The CanExecute callback in my CommandBinding is not be...

What use has RoutedCommand' class constructor ownertype argument?

The constructor of the RoutedCommand has "owner type" as a last argument. What is its significance? When it is used? MSDN documentation gives completely no clue about why it's needed and whether I could use one type for all commands Quote from MSDN ownerType Type: System.Type The type which is registering the command. The...

Executing WPF routed commands manually

When executing a custom RoutedUICommand manually from code-behind, like this: MyCommands.MyCommand.Execute(parameter, target) do I need to call the CanExecute method first or is this already done inside the Execute method? ...

Can't add a routed command to a CheckBox in WPF

Hi, A custom RoutedCommand is used by some menuItems; I would like to use the same RoutedCommand with checkboxes : <CheckBox Command="local:MainWindow.SwitchContextCommand"> The compiler gives no error, but when i lauch the app, I get an exception telling me that the Command value can't be null and consequently SwitchContextCommand c...

How to use the same RoutedCommand in distinct controls ?

Hi, I a have 2 distinct UI elements in distinct files : a menuItem (in window.xaml) and a userControl (in foo.xaml). Both have exactly the same logic : they are enabled only if a condition is matched and call the same method. To code this logic, i use RoutedCommands. I could use 2 different RoutedCommands in each file with same condit...

How to define a global custom RoutedCommand?

Hi, I would like to use the same custom RoutedCommand for 2 buttons, which are in separate windows. In order to not repeat the code, i want to define the command somewhere in the app and bind it to both buttons. I thought of using Style to achieve that. Below, I reproduce my issue in a simple sample. I declare the Style in App.Xaml :...

WPF TextBox Interceping RoutedUICommands

I am trying to get Undo/Redo keyboard shortcuts working in my WPF application (I have my own custom functionality implemented using the Command Pattern). It seems, however, that the TextBox control is intercepting my "Undo" RoutedUICommand. What is the simplest way to disable this so that I can catch Ctrl+Z at the root of my UI tree? I...

How to ovverride Copy command on WPF textbox?

Hello, I would like to override the behavior of RoutedUICommand "Copy" of a WPF TextBox. Is it possible without creating a new TextBoxExtended class that inherits from TextBox? I have reached that point, but now I am bit lost. Private Sub tbSource_PreviewExecuted(ByVal sender As System.Object, ByVal e As System.Windows.Input.Execu...

What is the difference between a RoutedCommand and a RoutedUICommand?

I have tried to do some reading about it, but I failed to find documentation that explains the difference between RoutedCommand and RoutedUICommand satisfactorily. ...

MVVM Command Routing Between Controls

I currently have a main View with a Button and a ContentPresenter which is bound to a property of the ViewModel which is another View (and associated ViewModel). Is there way to route a command from the a handler declared in the control loaded in the ContentPresenter? My reason for this is the main View contains the tool bar and the co...

How do I programatically fire a command

i have an ICommand that I want to fire (make the execute go) from code; how do I do this? ...