Handling Multiple Mouse Events in MVVM in WPF - mvvm

I am following the below discussion,
Handling Mouse Events in MVVM in WPF
It is working for one event but when I try to multiple events, it is not working.
I want to bind multiple events like below:
<i:Interaction.Triggers>
<i:EventTrigger
EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding TripLegCopyIconPressedCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding
TripLegPreviewMouseLeftButtonUpCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
But it is not working. How to do that? Any idea?
Thanks.

As per my understanding, multiple event binding will work and not recommended.

Related

Row Double Click in MVVM

Please help to create the Row Double Click event for datagrid using MVVM, currently i am using MouseDoubleClick event, using MouseDoubleClick event when we click anywhere on datagrid then that event is firing. Please help me to produce the event only when double click on rows.
Code that currently i am using i.e. MouseDoubleClick event:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding RowClick}" CommandParameter="{Binding ElementName=grdManageCab, Path=SelectedItem}">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
You are Subscribing the event on the DataGrid. And when you DoubleClick the entire Datagrid will fire the event. If you want that this event fire only on the Cells you must susbribe this evento on a DataGridCell
<DataGrid>
<DataGridCell>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding RowClick}" CommandParameter="{Binding ElementName=grdManageCab, Path=SelectedItem}">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGridCell>
</DataGrid>
For more info please here.
Hope helps. Greetings!

Force TextBox to lost focuse when EventToCommand fired

I need to perform login. I'm binding Login to my ViewModel. I can't bind password from passwordBox so I pass it through EventToCommand parametrer
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding LogInCommand,
Mode=OneWay}"
CommandParameter="{Binding Password,
ElementName=PasswordBox,
Mode=OneWay}"
/>
</i:EventTrigger>
</i:Interaction.Triggers>
My problem - when loginbox has focus the login in ViewModel is null. How to force lost focus when the button is tap.
I know that is not exactly the answer to your question, but still..
You can find here a realization of login dialog using MVVM
http://www.geoffhudik.com/tech/2012/2/19/windows-phone-login-navigation.html

No SelectionChanged event triggered in a ComboBox (WinRT with Windows.UI.Interactivity)

I am using recently released Windows.UI.Interactivity library to wire some events to MVVM commands in a WinRT app. It works fine for ListView, however no event is fired in a ComboBox. Here's a ComboBox control definition (some properties skipped for clarity):
<ComboBox
x:Name="collectionMode"
Margin="10"
SelectedIndex="0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding CollectionModeCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ComboBoxItem Content="Show collection properties" />
<ComboBoxItem Content="Show collection data" />
</ComboBox>
The CollectionModeCommand is never triggered (I have a similar wiring in a ListView and it works fine).
Any help is appreciated.
Resolved. The problem was with the incorrect binding of the outer control that of course resulted in the CollectionModeCommand not being bound to the ComboBox.

MVVM light EventToCommand: get selected item

I have a treeview where I can click on items. I am using MVVM light toolkit and the EventToCommand and would like to know which item was chosen. How can I pass this to my viewmodel?
<interact:Interaction.Triggers>
<interact:EventTrigger EventName="SelectedItemChanged">
<cmd:EventToCommand Command="{Binding Path=DataContext.SimpleCommand, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, Mode=OneWay}"
CommandParameter="{Binding SelectedItems, ElementName=mainTreeView}"
/>
</interact:EventTrigger>
</interact:Interaction.Triggers>
http://www.galasoft.ch/mvvm/#intro With the newest version, you can get the EventArgs of the fired event directly in the ViewModel to handle it.

Suggestions for "Blendable" ViewModelLocator with Unity 2.0

I have an suite of existing Silverlight applications using the MVVM pattern to separate Views and ViewModels. We use Unity 2.0 for an IoC container to inject dependencies into the ViewModel classes (and supporting types). I have an existing ViewModelLocator class that uses the Unity Container to resolve the ViewModel.
All of this is working great at runtime; however, because the ViewModelLocator relies on the Unity Container being created and configured by a Bootstrapper class that is "Run" from the Application_Start method in App.xaml.cs, I've lost the ability to open the views in the designer or in Blend.
I am looking for suggestions how I can rework the ViewModelLocator to support "Blendability".
Note that I'm not willing to force our ViewModel classes to implement default parameterless constructors just for the sake of Blendability. We also have our ViewModels check the IsInDesignMode property (from the MVVM Light ViewModelBase class) to supply design-time data versus making service calls so we don't have different ViewModel implementations for design-time and run-time.
Let me know what you think.
You want UI elements to have a good design-time experience (including in Blend). That sounds like a reasonable goal.
Let’s look at what a UI element is. For the rest of this answer I’m going to call it a Control. The responsibility of a Control is to render UI and respond to user events. Insofar as it should have behavior, it should only have behavior that relates to UI rendering and user events.
In addition to that, the framework itself (Silverlight as well as WPF) imposes a rule: all Controls must have a default constructor. Furthermore, since the DataContext is a property, it’s optional to assign it.
We should keep encapsulation in mind. Any Control we develop should work nicely within the constraints given above. This means that, apart from having a default constructor, it should also work correctly without a DataContext. In other words, the Blendability experience should be supplied by the Control itself, not any external container that needs to be bootstrapped to assign a DataContext.
When a Control must respond to user events I’ve always found the ICommand interface more than sufficient. Attach ICommands to any applicable event handler in the Control. The ICommands are defined by the View Model, but the beauty of ICommand is that it’s basically a void method which means that it’s trivial to supply a (no-op) Local Default in the case where the DataContext is null. However, that’s rarely necessary, as the Commands aren’t invoked by designers.
Here's an example from my book:
<Window x:Class="Ploeh.Samples.ProductManagement.WpfClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Product Management"
Height="300"
Width="300"
MinHeight="300"
MinWidth="300">
<Window.Resources>
<Style x:Key="ProductStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
</Style>
</Window.Resources>
<DockPanel FocusManager.FocusedElement="{Binding ElementName=productsListView}">
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<Separator />
<MenuItem Header="E_xit" Command="{Binding Path=CloseCommand}" />
</MenuItem>
<MenuItem Header="_Actions">
<MenuItem Header="_Refresh" InputGestureText="F5" Command="{Binding Path=RefreshCommand}" />
<MenuItem Header="_Add Product" InputGestureText="Ins" Command="{Binding Path=InsertProductCommand}" />
<MenuItem Header="_Edit Product" InputGestureText="Enter" Command="{Binding Path=EditProductCommand}" />
<MenuItem Header="_Delete Product" InputGestureText="Del" Command="{Binding Path=DeleteProductCommand}" />
</MenuItem>
</Menu>
<ToolBarTray DockPanel.Dock="Top" HorizontalAlignment="Stretch">
<ToolBar HorizontalAlignment="Stretch" HorizontalContentAlignment="Left">
<Button Command="{Binding Path=RefreshCommand}">Refresh</Button>
<Button Command="{Binding Path=InsertProductCommand}">Add</Button>
<Button Command="{Binding Path=EditProductCommand}">Edit</Button>
<Button Command="{Binding Path=DeleteProductCommand}">Delete</Button>
</ToolBar>
</ToolBarTray>
<ListView x:Name="productsListView" ItemContainerStyle="{StaticResource ProductStyle}" ItemsSource="{Binding Path=Products}" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Path=Id}" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" />
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Path=UnitPrice}" />
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</Window>
and the code-behind:
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
}