Select to view content in your preferred language

CommandParameter to get mouse click?

2706
2
04-08-2011 09:31 AM
LisaChesley
Emerging Contributor
Good afternoon, all!

I am trying to use the MVVM pattern in my silverlight application, and I'm trying to capture a mouse click on the map to use for identify.  I can capture the actual map mouse click using an InvokeCommandAction, but I can't seem to figure out how to pass the geometry of the mouse click as a command parameter to use in my view model.

Is there any way to capture the clicked point on the map?

I know how to do this using the code-behind on the view, so if no other solution exists, I can carry on doing it that way, but if there's a view model friendly version, I'd much rather use that!

Here is the XAML from the view for the map:

<esri:Map x:Name="map" Background="White" IsLogoVisible="False" Layers="{Binding MapLayerCollection}" viewModels:MapViewModel.BindableExtent="{Binding MapExtent}" Grid.RowSpan="2" >
    <i:Interaction.Triggers>
<i:EventTrigger EventName="MouseClick">
  <i:InvokeCommandAction Command="{Binding MapMouseClick, Mode=OneWay}" CommandParameter="<something to capture mouse click here>"/>
</i:EventTrigger>
    </i:Interaction.Triggers>
</esri:Map>

And here is the code from the view model:

public ICommand MapMouseClick { get; private set; }

public MapViewModel()
        {          
            //Set commands.      
            this.MapMouseClick = new DelegateCommand<object>(this.OnMapMouseClick, this.CanMapMouseClick);
         
        }

protected void OnMapMouseClick(object arg)
        {
            //Nothing is passed in to the arg, so have nothing to use here yet.
        }

protected bool CanMapMouseClick(object arg)
        {
            return true;
        }

If there's any other information I can provide, please let me know!

Thanks, and have a great day!

Lisa
0 Kudos
2 Replies
JenniferNery
Esri Regular Contributor
I think this Silverlight forum thread might help: http://forums.silverlight.net/forums/p/211425/498234.aspx
0 Kudos
LisaChesley
Emerging Contributor
Good afternoon, Jennifer!

Thank you for the prompt reply - that worked a treat!  I set up the MVVM Light framework, hooked up the dlls, and now my XAML looks like this:

<esri:Map x:Name="map" Background="White" IsLogoVisible="False" Layers="{Binding MapLayerCollection}" viewModels:MapViewModel.BindableExtent="{Binding MapExtent}" Grid.RowSpan="2" >  
   <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseClick">
     <GalaSoft_MvvmLight_Command:EventToCommand PassEventArgsToCommand="True" Command="{Binding MapMouseClick, Mode=OneWay}"/>
    </i:EventTrigger>
   </i:Interaction.Triggers>  
  </esri:Map>

And now in my code, I can do this:

public ICommand MapMouseClick { get; private set; }

public MapViewModel()
{
//Set commands.
this.MapMouseClick = new DelegateCommand<object>(this.OnMapMouseClick, this.CanMapMouseClick);

}

protected void OnMapMouseClick(object arg)
{
ESRI.ArcGIS.Client.Map.MouseEventArgs e = arg as ESRI.ArcGIS.Client.Map.MouseEventArgs;
}

protected bool CanMapMouseClick(object arg)
{
return true;
}

And I have access to the map point I need!

Thanks very much for your help!

Lisa
0 Kudos