WPF Bingmaps pushpin event binding mvvm - wpf

I would like to trigger a command in my ViewModel when the pushpin is clicked on the map. How can I achieve this using databinding?
Here is the DataTemplate I am using for the pinpoints:
<DataTemplate x:Key="PushPinTemplate">
<map:Pushpin Cursor="Hand"
map:MapLayer.Position="{Binding Location}">
</map:Pushpin>
</DataTemplate>

A colleague found the solution:
After adding the Nuget Package System.Windows.Interactivity.WPF
and adding the xml namespace
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
you can add an EventTrigger to the template, here the full template code, where
CachePushPinClicked is an ICommand
<DataTemplate x:Key="PushPinTemplate">
<map:Pushpin Cursor="Hand"
map:MapLayer.Position="{Binding Location}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding CachePushPinClicked}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</map:Pushpin>
</DataTemplate>

Related

MVVM Light, Mahapps metro and EventToCommand via EventTrigger from System.Windows.Interactivity

I'm going nuts on this one:
I have an application using both Mahapps.Metro as well as MVVMLight.
Basically most things are ok UNTIL you try to use Interaction.Triggers.
I typically end up with errors like
Cannot add instance of type 'EventToCommand' to a collection of type 'TriggerActionCollection'. Only items of type 'T' are allowed.
A way to reproduce this is via a repo I found online:
https://github.com/mike-schulze/mahapps-mvvmlight-setup
(sorry mike for hijacking) and adding a metro SplitButton to the viewmodel:
<Controls:SplitButton ItemsSource="{Binding MyList}" Grid.Column="1" Grid.Row="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Controls:SplitButton>
And MyList being
public List<string> MyList
{
get
{
List<string> theList = new List<string>();
theList.Add("Hello");
theList.Add("World");
return theList;
}
}
I'm not sure how many different namespaces, hard coded Interactivity versions in App.config or what so ever I tried.
Has anyone a working example with Mahapps Metro (version not too old) and MVVM Light and the EventToCommand stuff?
I thought my problem is related to updates to Metro - and them using Behaviors now instead of Interactivity. I'm using the Interactivity dll version 4.5.0.0.
But even the old git I mentioned above shows the problem... And I can't find a working solution.
Any help is greatly appreciated.
Thanks
You need to add the namespace for System.Windows.Interactivity too.
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Because MVVMLight uses this version of Interactivity.
<mah:MetroWindow x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight"
...
DataContext="{Binding Main, Source={StaticResource Locator}}">
...
<mah:SplitButton ItemsSource="{Binding MyList}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<command:EventToCommand Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</mah:SplitButton>
...
</mah:MetroWindow>

Bind an ICommand to a TabItem WPF (MVVM)

I have a TabControl with a few TabItems. I want one of my TabItems to act as a button. When I click on the TabItem, I want it to execute a Command in my associated ViewModel. I have the following code in my View:
<TabItem Header="Manage Users" Visibility="{Binding IsAdmin, Converter={StaticResource VisibilityOfBool}}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding Path=OpenLoginCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TabItem>
The OpenLoginCommand is an ICommand in the ViewModel. I have the interactivity namespace defined. What am I missing here?
Try PreviewMouseLeftButtonDown
<TabItem Header="Manage Users" Visibility="{Binding IsAdmin, Converter={StaticResource VisibilityOfBool}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding Path=OpenLoginCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TabItem>
Try using MouseDown instead of MouseLeftButtonDown as referencing MSDN the latter event doesn't exist on a TabItem control.
If your requirements insist on left-button only, then check the state of the mouse within the command.

How to bind a Command to SelectedItemChanged event of a TreeView

There is a Treeview Control.
<TreeView Name="ProductsHierarchy" FontFamily="Arial"
Background="White" Margin="2"
FontSize="12" SelectedItemChanged ="ProductsHierarchy_SelectedItemChanged">
Is there a way to bind a command for SelectedItemChanged event of the treeview, avoiding the code behind event handler?
Try MVVM Toolkit's EventToCommand.
"Built-in" (from Blend) approach is to use Interactivity
<TreeView Name="ProductsHierarchy" FontFamily="Arial"
Background="White" Margin="2"
FontSize="12" SelectedItemChanged ="ProductsHierarchy_SelectedItemChanged">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}" CommandParameter="argument"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
You must include namespace:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
The disadvantage here is that you have no access to EventArgs. Here's the solution (it's in Polish, but code samples are pretty much self-explanatory).

Routed Events of TextBlock

How to fire RoutedEvents of TextBlock in ViewModel from code behind in wpf.
Kindly let me know, how I can bind routed events in wpf code behind. Thanks
Well I use this (you will need System.Windows.Interactivity in Silverlight, but I suspect it's similar in WPF):
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<TextBlock Text="{Binding InputValue, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding InputValueGotFocusCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
You can do this easy in Blend

How do you use mvvm-light to trigger gotfocus

Using silverlight, we are looking to move the focus to a textbox.
How do you use mvvm-light to trigger gotfocus?
The view contains:
<TextBox Margin="4,4,0,0" Text="{Binding Path=SearchOID, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<cmd:EventToCommand Command="{Binding GotFocusCommand, Mode=TwoWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
How should the ViewModel look?
How do you trigger this from the ViewModel?
Ended up using a TriggerAction, very similar to:
http://www.codeproject.com/Articles/42988/Silverlight-Behaviors-and-Triggers-Making-a-Trigge.aspx

Resources