MVVM Multitouch command binding - wpf

I would like to create a WPF dialog where the user has to press (at least) two buttons before a command is executed.
I am able to create this using code-behind (touch events), but prefer to use MVVM command binding and a dedicated user control so it can be reused in other applications.
How can I activate the ICommand when both buttons are pressed simultaneously?

Related

Put RadGrid in insert mode via command

I need to put the grid in insert mode when a user clicks an Add button (he doesn't want the click on the grid, but an external button)
Since I've developed all my app using MVVM pattern, I was wondering if there's a way I can put the grid in insert mode via command
Any suggestion?
Thanks
The RadGridView comes with some built in commands, one of which is begininsert.
There's sample code shows how to invoke such commands on telerik's site:
https://docs.telerik.com/devtools/wpf/controls/radgridview/commands/overview
Since this is manipulating the state of a control then you can use code behind in the view and a click event without "breaking" mvvm. You can't run automated tests on this processing using just the viewmodel anyhow - since the viewmodel isn't going to know anything about a gridview, let alone any mode it's in.

How to Route a Button command from a View to numerous ViewModels

I am using the MVVM pattern in my WPF application. In one of my Views I have a button that when clicked uses Commands to talk to its ViewModel. The problem I have is that I need the ViewModel to then talk to other ViewModels to call some of their public methods. I use IOC (Unity) and inject the container into the first ViewModel, so could access the others by using this. I’m not sure if this fits in with the MVVM concept.
Is it possible for all my ViewModels to somehow subscribe to the one button click?
Are any of these the recommended way of solving this problem or is there a better way?
To explain a bit more about my application, each view is a tab control with several textboxes. On the first tab there is also a button and combobox. The user is free to enter their own data or select an option from the combo. In this instance, if the button is then clicked I need all the tabs to load their textboxes based on the selected item in the combo from the first tab. I somehow need to wire this button click in such a way that the value from the combo is passed to all the related viewmodels.
You can use the EventAggregator. Have the command publish an event that the other ViewModels can subscribe to.
When the event is raised they'll all get the event, without needing for one VM to know the other VMs
Another option is to use Composite Commands instead of a regular command.
Make the command the button uses a composite command, and have the other viewmodels register to that Composite command.
You could go a few ways with this one:
use some kind of eventing framework to notify all subscribers if something happens: eg Prism EventAggregator. For this to work you'll need to set up Prism obviously. There are other (MVVM) frameworks out there which support some kind of event/message system like Caliburn.Micro or MVVMLight
Create a MasterViewModel that contains all the child viewmodels for all the tabs. This way the 'master' can subscribe to the PropertyChanged events from its children and execute the appropriate actions. Or the Master can even contain the commands which you are binding to.
I would recommend using some form of "Messenger" service. By this, I mean a class that implements the "Subscription Pattern."
I'm not sure which MVVM library you might be using, but if you look at the "MVVM Light Toolkit" - which is available on CodePlex - you will find a very light implementation of a Messenger there.
Basically each ViewModel will subscribe to receive a specific notification and your ViewModel with the combo box and button will publish the message when the button is clicked. It is really quite flexible in how you send the messages and your ViewModels don't need to know anything about each other.
If you are using the MVVM lite toolkit from GalaSoft you have access to the Messenger which would allow you to send a message that you can subscribe to in each of your view models.
// Send message from command handler
Messenger.Default.Send<MyMessage>(new MyMessage());
// Register for message in view models.
Messenger.Default.Register<MyMessage>(this, MyMessageReceived);
// Method to do work
private void MyMessageReceived(MyMessage myMessage)
{
// Do Work
}

How to execute a command and change focus after a keypress in a WPF app

I have a WPF app that uses MVVM Light and I wish to both execute a command on the view model and change the keyboard focus to a specific control when the user presses ALT+SHIFT+C.
Is it possible to achieve this in an elegant way?
It depends on how the shortcut key is created (if it's like Visual Studio or more like Windows - it means if you have to hold only ALT or all the keys).
But whatever the logic, You will have to first bind an Event to a Command (it might be the event keydown of one of your controls).
In MVVM Light, you will have to use Interaction.Triggers with EventToCommand (there's a lot ot explanations on google and SO)
The logic would be put here in you command.
Then a dependecy property as show here could be implemented for getting the focus.

C# WPF ribbon:RibbonTextBox vs. TextBox

After installing Microsoft Ribbon for WPF, I create a WPF Ribbon Application. For textbox, I have choices of "ribbon:RibbonTextBox" or "TextBox" - they seems to work the same to me. Anyone know of the difference between the two?
The RibbonTextBox class implements ICommandSource, which allows it to integrate with the WPF command framework.
For instance, a command can be invoked when the text box has the input focus and the RETURN key is pressed, or the text box can disable itself when its associated command cannot be executed.

How to launch RoutedCommand on application level in multiple windows pattern?

I have a multiple windows in an application, for instance, window1, window2 and window3.
one RoutedCommand (with KeyGesture F11) was binded in window1.
How to launch that routed command by pressing F11, while window2 had input focus ?
In WinForm application, i use MessageFilter to detect F11, but in WPF, how to do that ?
You can use CommandManager.RegisterClassCommandBinding to hook up a handler to every Window application wide. This will be continue working for the rest of your application run so it usually makes sense to put it in App.xaml.cs but you could put it anywhere.
CommandManager.RegisterClassCommandBinding(typeof(Window), new CommandBinding(ApplicationCommands.Cut, CutExecuted));

Resources